Skip to content

Commit b36fc11

Browse files
gunhaxxordplewisTomWFox
authored
Clarifying documentation of custom authentication (parse-community#785)
* Clarifying documentation of custom authentication. Documentation on custom authentication was extremely unclear. Tried to clarify a few aspects. That id is required in the authData object. Also what is client-side vs backend code. Very open to feedback and improvements! * Update users.md * Apply suggestions from code review Co-authored-by: Diamond Lewis <[email protected]> Co-authored-by: Tom Fox <[email protected]>
1 parent bd221b7 commit b36fc11

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

_includes/js/users.md

+18-9
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ Our library manages the `FB` object for you. The `FB` singleton is synchronized
389389

390390
Parse allows you to link your users with [3rd party authentication]({{ site.baseUrl }}/parse-server/guide/#oauth-and-3rd-party-authentication), enabling your users to sign up or log into your application using their existing identities. This is accomplished through [`linkWith`](https://parseplatform.org/Parse-SDK-JS/api/2.9.0/Parse.User.html#linkWith) method by providing authentication data for the service you wish to link to a user in the `authData` field. Once your user is associated with a service, the `authData` for the service will be stored with the user and is retrievable by logging in.
391391

392-
`authData` is a JSON object with keys for each linked service containing the data below.
392+
`authData` is a JSON object with keys for each linked service containing the data below. The `authData` object is required to at least have a key named `id`, which is used to identify the user on subsequent login attempts with linked account data.
393393

394394
> `_linkWith` has been deprecated since version 2.9.0, see [_linkWith](https://parseplatform.org/Parse-SDK-JS/api/master/Parse.User.html#_linkWith)
395395
@@ -399,7 +399,7 @@ Signing a user up with a linked service and logging them in with that service us
399399

400400
```javascript
401401
const myAuthData = {
402-
id: '12345678'
402+
id: '12345678' // Required field. Used to uniquely identify the linked account.
403403
};
404404
const user = new Parse.User();
405405
await user.linkWith('providerName', { authData: myAuthData });
@@ -450,7 +450,7 @@ To create a link to an un-authenticated user (for example in cloud code), option
450450

451451
```javascript
452452
const myAuthData = {
453-
id: xzx5tt123,
453+
id: xzx5tt123, // The id key is required in the authData-object. Otherwise Parse Server will throw the Error 252 'This authentication method is unsupported'.
454454
access: token
455455
}
456456

@@ -473,14 +473,15 @@ const loggedIn = await Parse.User.logInWith('CustomAdapter', { authData: myAuthD
473473
### Custom Authentication Module
474474

475475
Parse Server supports many [3rd Party Authenications]({{ site.baseUrl }}/parse-server/guide/#oauth-and-3rd-party-authentication).
476-
It is possible to `linkWith` any 3rd Party Authentication by creating a custom authentication module.
476+
It is possible to `linkWith` any 3rd Party Authentication by creating a custom authentication module. A custom authentication module normally consists of a client-side AuthProvider object and a back-end AuthAdapter. The client-side object should implement the [AuthProvider interface](https://github.com/parse-community/Parse-SDK-JS/blob/master/src/interfaces/AuthProvider.js). The backend AuthAdapter should implement the the functions `validateAuthData` and `validateAppId`, check out this [AuthAdapter example](https://github.com/parse-community/parse-server/blob/master/src/Adapters/Auth/AuthAdapter.js).
477+
When calling the `linkWith` function **without** an `authData` object the client side authenticate-method from the provider object will be called. In the other case the `authData` object will be sent directly to Parse Server for authentication using the backend module.
477478

478-
[Read more about Auth Provider Documentation](https://github.com/parse-community/Parse-SDK-JS/blob/master/src/interfaces/AuthProvider.js)
479+
Note: The following is a minimal example implementing AuthProvider client-side and AuthAdapter on the backend.
479480

480-
Note: This is an example, you can handle your own authentication (if you don't have authData), restoreAuthentication and deauthenticate methods.
481-
482-
A minimal `CustomAuth.js` module:
481+
A minimal `CustomAuth.js` module in the backend:
483482
```javascript
483+
// Don't require or import parse-server in this module.
484+
// See this issue: https://github.com/parse-community/parse-server/issues/6467
484485
function validateAuthData(authData, options) {
485486
return Promise.resolve({})
486487
}
@@ -517,7 +518,15 @@ app.use('/parse', api);
517518
Use the `CustomAuth`:
518519
```javascript
519520
const provider = {
520-
authenticate: () => Promise.resolve(),
521+
authenticate: (options) => {
522+
// Some code to get retrieve authData
523+
// If retrieved valid authData, call success function with the data
524+
options.success(this, {
525+
id: 1234
526+
});
527+
// You can also handle error
528+
// options.error(this, {});
529+
},
521530
restoreAuthentication() {
522531
return true;
523532
},

0 commit comments

Comments
 (0)