Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor auth for consistency #1324

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
"src/js/base/**/*.js",
"src/js/auth.js",
"src/js/auth0.js",
"src/js/auth/*.js",
"src/js/formapp.js",
"src/js/formapp/**/*.js",
"src/js/formservice.js",
Expand Down
159 changes: 17 additions & 142 deletions src/js/auth.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to call the .should()'s only once for each auth type at the beginning of the file?

Something along the lines of this:

let authAgent;

if (kinde.should()) authAgent = kinde;
if (auth0.should()) authAgent = auth0;
if (workos.should()) authAgent = workos;

// use authAgent throughout file:
// authAgent.setToken()
// authAgent.getToken()
// etc.

May be slightly cleaner possibly.

Would allow us to add some guards to some of those functions with if (!authAgent) return ....

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good suggestion.. check out the latest

Original file line number Diff line number Diff line change
@@ -1,53 +1,35 @@
import { extend } from 'underscore';
import Radio from 'backbone.radio';
import createKindeClient from '@kinde-oss/kinde-auth-pkce-js';

import { kindeConfig as config, appConfig } from './config';

import { auth as auth0, logout as auth0Logout, setToken as auth0SetToken, getToken as auth0GetToken } from './auth0';
import * as auth0 from './auth/auth0';
import * as none from './auth/none';
import * as kinde from './auth/kinde';

import 'scss/app-root.scss';

import { LoginPromptView } from 'js/views/globals/prelogin/prelogin_views';
let authAgent;

const PATH_ROOT = '/';
const PATH_RWELL = '/rw';
const PATH_AUTHD = '/authenticated';
const PATH_LOGIN = '/login';
const PATH_LOGOUT = '/logout';
function getAuthAgent() {
if (none.should()) return none;

function shouldAuth0() {
return !config.createParams;
}
if (authAgent) return authAgent;

let kinde;
let token;
// These should be ordered by priority lowest to highest
if (auth0.should()) authAgent = auth0;
if (kinde.should()) authAgent = kinde;

// Sets a token when not using auth0;
function setToken(tokenString) {
if (shouldAuth0()) return auth0SetToken(tokenString);
return authAgent;
}

token = tokenString;
function setToken(tokenString) {
getAuthAgent().setToken(tokenString);
}

function getToken() {
if (shouldAuth0()) return auth0GetToken();

if (token) return token;
if (!kinde || !navigator.onLine) return;

return kinde
.getToken()
.catch(() => {
logout();
});
getAuthAgent().getToken();
}

function logout() {
if (shouldAuth0()) return auth0Logout();

token = null;
window.location = '/logout';
getAuthAgent().logout();
}

Radio.reply('auth', {
Expand All @@ -56,115 +38,8 @@ Radio.reply('auth', {
getToken,
});

/*
* Modifies the current history state
*/
function replaceState(state) {
window.history.replaceState({}, document.title, state);
}

function registerKinde(path, connection) {
kinde.register({
app_state: { path },
authUrlParams: { connection_id: connection },
});
}

function login(path = PATH_ROOT, connection = config.connections.default) {
// iframe buster
if (top !== self) {
top.location = PATH_LOGIN;
return;
}

replaceState(PATH_LOGIN);

if (appConfig.disableLoginPrompt) {
registerKinde(path, connection);
return;
}

const loginPromptView = new LoginPromptView();

loginPromptView.on('click:login', ()=> {
registerKinde(path, connection);
});

loginPromptView.render();
}

function shouldAuth() {
if (appConfig.cypress) {
setToken(appConfig.cypress);
return false;
}

if (!navigator.onLine) {
return false;
}

return true;
}

async function createKinde(success) {
const kindeCreateParams = {
redirect_uri: location.origin + PATH_AUTHD,
logout_uri: location.origin,
on_redirect_callback: (user, { path } = {}) => {
if (!user) {
login(path);
return;
}

if (path === PATH_LOGIN) path = PATH_ROOT;

if (path === PATH_RWELL) {
path = PATH_ROOT;
localStorage.setItem(PATH_RWELL, 1);
}

replaceState(path);

success();
},
};

return createKindeClient(extend(kindeCreateParams, config.createParams));
}

/*
* Requests kinde authorization
* And authenticates authorization if kinde redirected to PATH_AUTHD
*/
async function auth(success) {
if (!shouldAuth()) return success();

if (shouldAuth0()) return auth0(success);

// NOTE: Set path before await create to avoid redirect replaceState changing the value
const pathName = location.pathname;

kinde = await createKinde(success);

if (pathName === PATH_AUTHD) return;

if (pathName === PATH_LOGOUT) {
kinde.logout();
return;
}

// RWell specific login
if (pathName === PATH_RWELL || localStorage.getItem(PATH_RWELL)) {
login(PATH_RWELL, config.connections.roundingwell);
return;
}

if (!await kinde.getUser()) {
login(pathName);
return;
}

success();
getAuthAgent().auth(success);
}

export {
Expand Down
Loading
Loading