Login with OAuth2 Providers
The following code contains example use cases for authentication flows using OAuth2 with Google / Facebook / Microsoft. Currently only support Google
Login with credentials from Google
Step 1: Authenticate with Google
To authenticate with Google, you will need to use the Google Sign-In API to retrieve a Google ID token (or access-token) for the user. You can then use this token to authenticate the user with the server.
Here is document how to Verify the Google ID token on your server side
{
"envelope": {
"alg": "RS256",
"kid": "acda360fb36cd15ff83af8...",
"typ": "JWT"
},
"payload": {
"iss": "https://accounts.google.com",
"azp": "454...67pgst.apps.googleusercontent.com",
"aud": "454...67pgst.apps.googleusercontent.com",
"sub": "107...195",
"email": "be...nt@gmail.com",
"email_verified": true,
"at_hash": "iSfgM...rQ",
"name": "An Ngo",
"picture": "https://lh3.googleusercontent.com/a/AGN...96-c",
"given_name": "The",
"family_name": "Name",
"locale": "en",
"iat": 1680971466,
"exp": 1680975066
}
}
Step 2: Log in with OAuth Provider
Once you have authenticated the user with Google, you can use their Google account to log in to your application. To do this, you will need to use an OAuth provider like Google to create a user account on your server.
- Call
POST /auth/loginwith the following request body:
{
"type": "oauth-provider",
"clientId": "client-app-id",
"withOAuth": {
"providerName": "18kings-account-web.google",
"providerToken": "4543091062...t67pgst.apps.googleusercontent.com",
"providerTokenType": "idtoken"
}
}
Properties
| Field | Description |
|---|---|
providerName | provider name that pre-config in backend that associated with a Google clientId and secret. |
providerToken | Token that web / mobile client get from user, can be an id-token or access-token. |
providerTokenType | value: 'idtoken' or 'accesstoken' to match token providerToken value above |
- Expect a
200 OKresponse with anaccessTokenproperty.
{
"accessToken": "...",
"refreshToken": "..."
}
Step 3: Update user profile
After logging in with the OAuth provider, you may need to update the user's profile with additional information (e.g. email address, name, etc.). This can be done using an API endpoint on your server.
After update email / username / password, user can login by usename and password instead.
Step 4: Update user email
- Call
PATCH /api/me/emailwith the following request body:
{
"email": "your-new-email@gmail.com"
}
- Call
GET /auth/confirm-email/:verifyToken, where:verifyTokenis the verification token sent to the user's email address. - Expect a
200 OKresponse with the following body:
{
"success": true,
"message": "Email verified"
}
Step 5: Update user password
- Call
PATCH /api/me/passwordwith the following request body:
{
"currentPassword": "",
"password": "your-new-password",
"passwordConfirm": "your-new-password"
}
In first password changes request, current password allow empty
Password must follow password rules and must includes uppercase and special characters.
/((?=._\d)|(?=._\W+))(?![.\n])(?=._[A-Z])(?=._[a-z]).\*$/
- Expect a
200 OKresponse with the following body:
{
"success": true,
"message": "Password updated"
}
Step 5: Update user username
Username only allow to changes in limited times, currently 1
- Call
PATCH /api/me/usernamewith the following request body:
{
"username": "your-new-username"
}
- Expect a
200 OKresponse with the following body:
{
"success": true,
"message": "Username updated"
}
Step 5: Use Access Token
Once the user's profile has been updated, you can use the access token to authenticate subsequent requests to your server.
Intergate login buttons with provider (Google, Facebook...)
Draft guide to integrate external provider login button (currently only support Google)
Pure provider login button
Use this method require immerdiate step to use client library that login provider provide then use credential from client library to login
- Step 1: Using provider-based client login library to login and directly get provider access token, suggestion some guide below:
- Step 2: Using provider-based access token to login with
POST /auth/loginas above guide
Use api server as direct login
Using this method to directly login into service, only available on webapp since this require redirection that only available when call on browser
(to be implemented)
Related API Endpoints
POST /api/auth/login: Login user.PATCH /api/me/email: Update user email.PATCH /api/me/username: Update user username.PATCH /api/me/password: Update user password.