Skip to content

Instantly share code, notes, and snippets.

@rhosys-service-account
Last active November 21, 2020 10:35
Show Gist options
  • Save rhosys-service-account/1e123ba3fbec9d446527d67d27562b45 to your computer and use it in GitHub Desktop.
Save rhosys-service-account/1e123ba3fbec9d446527d67d27562b45 to your computer and use it in GitHub Desktop.
Google Oauth identity provider quick start guide
const loginClient = new LoginClient({
// Both of these properties an be found and configured at:
// https://authress.io/app/#/manage?focus=applications
authenticationServiceUrl: 'https://login.application.com',
applicationId: 'YOUR_APPLICATION_ID'
});
// This will redirect the user to Google, track their session with Authress,
// and then redirect back to your specified redirectUrl. By default we redirect them back
// to where they started, however usually you’ll want to specify the next location
// they should go.
await loginClient.authenticate({
connectionId: 'SELECTED_CONNECTION_ID', redirectUrl: window.location.href
});
// Once the user returns to your site, you’ll want to get the necessary credentials.
// These should always be fetched by using the token getter:
const userToken = await loginClient.getToken();
// The full flow is
// 1. Check if the user is logged in on every page.
// * If not redirect them to the login page, a route guard is the best way to handle.
// * If there are credentials, get the token and the loop is complete
// 2. Ask the user to select connection/provider, call authenticate
// 3. Get the token from the SDK when you need it
// * and pass it in the Authorization header in HTTP API requests.
const isUserLoggedIn = await loginClient.userSessionExists();
if (!isUserLoggedIn) {
window.location.assign('/login');
} else {
const userToken = await loginClient.getToken();
}
// If route === 'Login'
await loginClient.authenticate({
connectionId: 'SELECTED_CONNECTION_ID', redirectUrl: window.location.href
});
// After redirect, same check, but now we'll have the token.
const isUserLoggedIn = await loginClient.userSessionExists();
if (!isUserLoggedIn) {
window.location.assign('/login');
} else {
const userToken = await loginClient.getToken();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment