From 321a2ece65938264fec0249b40009a31568e0d7e Mon Sep 17 00:00:00 2001 From: Robert Bo Davis Date: Fri, 26 Apr 2024 10:58:06 -0400 Subject: [PATCH] chore: add ability to auto verify and auto login in test env --- remix/.env.example | 13 +++++++++++++ remix/app/.server/config.ts | 12 ++++++++++++ remix/app/.server/services/auth.ts | 19 +++++++++++++++---- remix/app/.server/services/user.ts | 10 +++++++--- remix/app/routes/logout/action.server.ts | 14 +++----------- remix/prisma/seeds/users.js | 4 ++-- 6 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 remix/.env.example diff --git a/remix/.env.example b/remix/.env.example new file mode 100644 index 0000000..b69d7fd --- /dev/null +++ b/remix/.env.example @@ -0,0 +1,13 @@ +DATABASE_URL="postgresql://codellm:password@localhost:5432/codellm?schema=public" + +AUTH0_CALLBACK_URL="http://localhost:3000/callback" +AUTH0_CLIENT_ID="your_client_id_here" +AUTH0_CLIENT_SECRET="your_client_secret_here" +AUTH0_DOMAIN="your_domain_here" +AUTH0_LOGOUT_URL="https://your_domain_here/v2/logout" + +# Set to "true" to automatically verify users when they sign up +USER_AUTO_VERIFY="true" + +# Set to "true" to automatically log in the mock user +USER_AUTO_LOGIN="true" diff --git a/remix/app/.server/config.ts b/remix/app/.server/config.ts index 9f84fe0..80b2236 100644 --- a/remix/app/.server/config.ts +++ b/remix/app/.server/config.ts @@ -12,9 +12,15 @@ export type Auth0Config = { secrets: string; }; +export type UserConfig = { + userAutoLogin: boolean; + userAutoVerify: boolean; +}; + export type Config = { auth0: Auth0Config; codellm: PartialConfig; + user: UserConfig; }; let config: Config; @@ -110,6 +116,11 @@ export const getAuth0Config = () => secrets: process.env.AUTH0_SECRETS ?? '', }) as Auth0Config; +export const getUserConfig = () => ({ + userAutoLogin: process.env.USER_AUTO_LOGIN === 'true', + userAutoVerify: process.env.USER_AUTO_VERIFY === 'true', +}); + export const initConfig = () => { config = remember( 'config', @@ -117,6 +128,7 @@ export const initConfig = () => { ({ auth0: getAuth0Config(), codellm: getCodellmConfig(), + user: getUserConfig(), }) as Config, ); }; diff --git a/remix/app/.server/services/auth.ts b/remix/app/.server/services/auth.ts index 71199da..b855c30 100644 --- a/remix/app/.server/services/auth.ts +++ b/remix/app/.server/services/auth.ts @@ -38,13 +38,15 @@ const auth0Strategy = new Auth0Strategy( } const user = await userModel.getByAuth0Id(profile.id); - if (user) return profile; + if (!isError(user)) return profile; + if (!isError(user, 'userModel:notFound')) throw user; const createRes = await userModel.create({ auth0Id: profile.id, email: profile.emails?.[0].value || '', firstName: profile.displayName?.split(' ')[0], lastName: profile.displayName?.split(' ')[-1], + isVerified: getConfig('user.userAutoVerify'), }); if (isError(createRes)) throw createRes; @@ -66,11 +68,11 @@ export const getSession = async ({ request }: ServiceCommonParams) => { return session; }; -export type GetLogoutURLParams = ServiceCommonParams & { - returnToPath: string; +export type LogoutParams = ServiceCommonParams & { + returnToPath?: string; }; -export const getLogoutURL = ({ request, returnToPath }: GetLogoutURLParams) => { +export const getLogoutURL = ({ request, returnToPath = '/' }: LogoutParams) => { // Parse the request URL to get the origin and replace the path const url = new URL(request.url); const returnToURL = new URL(returnToPath, url.origin); @@ -93,7 +95,16 @@ export const getLogoutOptions = async (params: ServiceCommonParams) => { }; }; +export const logout = async (params: LogoutParams) => { + const logoutUrl = getLogoutURL(params); + const logoutOptions = await getLogoutOptions(params); + if (isError(logoutOptions)) throw redirect('/'); + + throw redirect(logoutUrl, logoutOptions); +}; + export const getAuthProfile = async ({ request }: ServiceCommonParams) => { + if (getConfig('user.userAutoLogin')) return { id: 'mock-user' }; const session = await getSession({ request }); if (!session) return null; diff --git a/remix/app/.server/services/user.ts b/remix/app/.server/services/user.ts index 9f95600..b2fa652 100644 --- a/remix/app/.server/services/user.ts +++ b/remix/app/.server/services/user.ts @@ -2,8 +2,9 @@ import type { ServiceCommonParams } from './types'; import type { UserModel } from '@remix/.server/models'; import type { RemixError } from '@remix/.server/errors'; +import { redirect } from '@remix-run/node'; import { userModel } from '@remix/.server/models'; -import { getAuthProfile } from '@remix/.server/services/auth'; +import { getAuthProfile, logout } from '@remix/.server/services/auth'; import { isError, newError } from '@remix/.server/errors'; export const ERRORS = { @@ -20,7 +21,6 @@ export const ERRORS = { export const getUser = async (params: ServiceCommonParams) => { const authUser = await getAuthProfile(params); - if (!authUser) { return newError({ code: 'userService:noAuthUser', @@ -28,7 +28,11 @@ export const getUser = async (params: ServiceCommonParams) => { }); } - return userModel.getByAuth0Id(authUser.id); + const user = await userModel.getByAuth0Id(authUser.id); + if (isError(user, 'userModel:notFound')) return logout(params); + if (isError(user)) throw user; + + return user; }; export const validateUser = (user: UserModel | RemixError) => { diff --git a/remix/app/routes/logout/action.server.ts b/remix/app/routes/logout/action.server.ts index 767cc94..9433e35 100644 --- a/remix/app/routes/logout/action.server.ts +++ b/remix/app/routes/logout/action.server.ts @@ -1,15 +1,7 @@ import type { ActionFunctionArgs } from '@remix-run/node'; -import { redirect } from '@remix-run/node'; -import { getLogoutOptions, getLogoutURL } from '@remix/.server/services/auth'; -import { isError } from '@remix/.server/errors'; +import { logout } from '@remix/.server/services/auth'; -export const action = async ({ request }: ActionFunctionArgs) => { - const logoutUrl = getLogoutURL({ request, returnToPath: '/' }); - const logoutOptions = await getLogoutOptions({ request }); - if (isError(logoutOptions)) { - return redirect('/'); - } - - throw redirect(logoutUrl, logoutOptions); +export const action = async (params: ActionFunctionArgs) => { + return logout(params); }; diff --git a/remix/prisma/seeds/users.js b/remix/prisma/seeds/users.js index 3dcc9ce..c5e016b 100644 --- a/remix/prisma/seeds/users.js +++ b/remix/prisma/seeds/users.js @@ -1,7 +1,7 @@ const users = [ { - auth0Id: 'github|7569921', - email: 'bo@interrobang.consulting', + auth0Id: 'mock-user', + email: 'bo+codellm-mock@interrobang.consulting', firstName: 'Bo', isVerified: true, lastName: 'Davis',