Autenticación Social
FoalTS social authentication is based on OAuth2 protocol. To set up social authentication with Foal, you first need to register your application to the social provider you chose (Google, Facebook, etc). This can be done through its website.
Usually your are required to provide:
- an application name,
- a logo,
- and redirect URIs where the social provider should redirect the users after successful authentication (ex:
http://localhost:3001/signin/google/callback
,https://example.com/signin/facebook/callback
)
Once done, you should receive:
- a client ID that is public and identifies your application,
- and a client secret that must not be revealed and can therefore only be used on the backend side.
You must obtain a client secret. If you do not have one, it means you probably chose the wrong option at some point.
#
ConfigurationFirst install the appropriate package.
Then, you will need to provide your client ID, client secret and your redirect URIs to Foal. This can be done through the usual configuration files.
default.yml
production.yml
.env
#
Social ProvidersAuthentication is managed by social providers. These are services whose methods can be called within a controller to build a social login.
You can also override the scopes in the redirect
method:
Additional parameters can passed to the redirect
and getUserInfo
methods depending on the provider.
#
GoogleService name | Default scopes | Available scopes |
---|---|---|
GoogleProvider | openid , profile , email | Google scopes |
#
Register an OAuth applicationVisit the Google API Console to obtain a client ID and a client secret.
#
Redirection parametersThe redirect
method of the GoogleProvider
accepts additional parameters. These parameters and their description are listed here and are all optional.
Example
#
FacebookService name | Default scopes | Available scopes |
---|---|---|
FacebookProvider | email | Facebook permissions |
#
Register an OAuth applicationVisit Facebook's developper website to create an application and obtain a client ID and a client secret.
#
Redirection parametersThe redirect
method of the FacebookProvider
accepts an additional auth_type
parameter which is optional.
Example
Name | Type | Description |
---|---|---|
auth_type | 'rerequest' | If a user has already declined a permission, the Facebook Login Dialog box will no longer ask for this permission. The auth_type parameter explicity tells Facebook to ask the user again for the denied permission. |
#
User info parametersThe getUserInfo
and getUserInfoFromTokens
methods of the FacebookProvider
accept an additional fields
parameter which is optional.
Example
Name | Type | Description |
---|---|---|
fields | string[] | List of fields that the returned user info object should contain. These fields may or may not be available depending on the permissions (scopes ) that were requested with the redirect method. Default: ['id', 'name', 'email']. |
#
GithubService name | Default scopes | Available scopes |
---|---|---|
GithubProvider | none | Github scopes |
#
Register an OAuth applicationVisit this page to create an application and obtain a client ID and a client secret.
Additional documentation on Github's redirect URLs can be found here.
#
Redirection parametersThe redirect
method of the GithubProvider
accepts additional parameters. These parameters and their description are listed below and are all optional.
Example
Name | Type | Description |
---|---|---|
login | string | Suggests a specific account to use for signing in and authorizing the app. |
allow_signup | boolean | Whether or not unauthenticated users will be offered an option to sign up for GitHub during the OAuth flow. The default is true . Use false in the case that a policy prohibits signups. |
Source: https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/#parameters
#
LinkedInService name | Default scopes | Available scopes |
---|---|---|
LinkedInProvider | r_liteprofile | API documentation |
#
Register an OAuth applicationVisit this page to create an application and obtain a client ID and a client secret.
#
User info parametersThe getUserInfo
and getUserInfoFromTokens
methods of the LinkedInProvider
accept an additional projection
parameter which is optional.
Example
Name | Type | Description |
---|---|---|
fields | string[] | List of fields that the returned user info object should contain. Additional documentation on field projections. |
projection | string | LinkedIn projection parameter. |
#
Sign In and Sign Up ExampleThis example shows how to implement a sign in and sign up flow with sessions and a TypeORM User
entity.
user.entity.ts
auth.controller.ts
#
Custom ProviderIf necessary, you can also implement your own provider. This one must inherit the abstract class AbstractProvider
.
Example