update auth packages
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { createContext, redirect, type LoaderFunctionArgs } from 'react-router'
|
import { createContext, redirect, type LoaderFunctionArgs } from 'react-router'
|
||||||
|
|
||||||
import { userContext } from '@repo/auth/context'
|
import { requestIdContext, userContext } from '@repo/auth/context'
|
||||||
import { request as req } from '@repo/util/request'
|
import { request as req } from '@repo/util/request'
|
||||||
|
|
||||||
import { CACHE_NAME, CACHE_TTL_SECONDS } from '@/conf'
|
import { CACHE_NAME, CACHE_TTL_SECONDS } from '@/conf'
|
||||||
@@ -42,18 +42,18 @@ export const workspaceMiddleware = async (
|
|||||||
next: () => Promise<Response>
|
next: () => Promise<Response>
|
||||||
): Promise<Response> => {
|
): Promise<Response> => {
|
||||||
const orgId = params.orgid
|
const orgId = params.orgid
|
||||||
|
const requestId = context.get(requestIdContext)
|
||||||
const user = context.get(userContext)!
|
const user = context.get(userContext)!
|
||||||
|
|
||||||
const cacheKey = buildWorkspaceCacheKey(request, user.sub, orgId)
|
const cacheKey = buildWorkspaceCacheKey(request, user.sub, orgId)
|
||||||
const cached = await getWorkspaceFromCache(cacheKey)
|
const cached = await getWorkspaceFromCache(cacheKey)
|
||||||
|
|
||||||
if (cached) {
|
if (cached) {
|
||||||
console.log('Warm start')
|
|
||||||
context.set(workspaceContext, cached)
|
context.set(workspaceContext, cached)
|
||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Cache miss')
|
console.log(`[${new Date().toISOString()}] [${requestId}] Cache miss`)
|
||||||
|
|
||||||
const r = await req({
|
const r = await req({
|
||||||
url: `/users/${user.sub}/orgs?limit=25`,
|
url: `/users/${user.sub}/orgs?limit=25`,
|
||||||
@@ -139,7 +139,7 @@ async function saveToCache(
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const cache: Cache = await caches.open(CACHE_NAME)
|
const cache: Cache = await caches.open(CACHE_NAME)
|
||||||
|
|
||||||
const response: Response = new Response(JSON.stringify(workspace), {
|
const response = new Response(JSON.stringify(workspace), {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Cache-Control': `public, max-age=${CACHE_TTL_SECONDS}`
|
'Cache-Control': `public, max-age=${CACHE_TTL_SECONDS}`
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import type { Route } from './+types/route'
|
|||||||
import { redirect } from 'react-router'
|
import { redirect } from 'react-router'
|
||||||
|
|
||||||
import { createAuth, type User } from '@repo/auth/auth'
|
import { createAuth, type User } from '@repo/auth/auth'
|
||||||
import { requestIdContext, cloudflareContext } from '@repo/auth/context'
|
import { cloudflareContext, requestIdContext } from '@repo/auth/context'
|
||||||
import { createSessionStorage } from '@repo/auth/session'
|
import { createSessionStorage } from '@repo/auth/session'
|
||||||
|
|
||||||
export async function loader({ request, context }: Route.LoaderArgs) {
|
export async function loader({ request, context }: Route.LoaderArgs) {
|
||||||
@@ -13,6 +13,7 @@ export async function loader({ request, context }: Route.LoaderArgs) {
|
|||||||
const sessionStorage = createSessionStorage(cloudflare.env)
|
const sessionStorage = createSessionStorage(cloudflare.env)
|
||||||
const session = await sessionStorage.getSession(request.headers.get('cookie'))
|
const session = await sessionStorage.getSession(request.headers.get('cookie'))
|
||||||
const user = session.get('user')
|
const user = session.get('user')
|
||||||
|
const now = new Date().toISOString()
|
||||||
const returnTo = (session.get('returnTo') as string | undefined) ?? '/'
|
const returnTo = (session.get('returnTo') as string | undefined) ?? '/'
|
||||||
|
|
||||||
if (user) {
|
if (user) {
|
||||||
@@ -26,9 +27,8 @@ export async function loader({ request, context }: Route.LoaderArgs) {
|
|||||||
request
|
request
|
||||||
)) as User
|
)) as User
|
||||||
session.set('user', authenticatedUser)
|
session.set('user', authenticatedUser)
|
||||||
session.unset('returnTo')
|
|
||||||
|
|
||||||
console.log(`[${requestId}] Redirecting the user to ${returnTo}`)
|
console.log(`[${now}] [${requestId}] Redirecting the user to ${returnTo}`)
|
||||||
|
|
||||||
// Redirect to the home page after successful login
|
// Redirect to the home page after successful login
|
||||||
return redirect(returnTo, {
|
return redirect(returnTo, {
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ import { decodeJwt } from 'jose'
|
|||||||
import { redirect, type LoaderFunctionArgs } from 'react-router'
|
import { redirect, type LoaderFunctionArgs } from 'react-router'
|
||||||
import type { OAuth2Strategy } from 'remix-auth-oauth2'
|
import type { OAuth2Strategy } from 'remix-auth-oauth2'
|
||||||
|
|
||||||
import { requestIdContext, userContext, cloudflareContext } from '../context'
|
|
||||||
import { createSessionStorage } from '../session'
|
|
||||||
import { createAuth, type User } from '../auth'
|
import { createAuth, type User } from '../auth'
|
||||||
|
import { cloudflareContext, requestIdContext, userContext } from '../context'
|
||||||
|
import { createSessionStorage } from '../session'
|
||||||
|
|
||||||
export const authMiddleware = async (
|
export const authMiddleware = async (
|
||||||
{ request, context }: LoaderFunctionArgs,
|
{ request, context }: LoaderFunctionArgs,
|
||||||
@@ -16,12 +16,14 @@ export const authMiddleware = async (
|
|||||||
const strategy = authenticator.get<OAuth2Strategy<User>>('oidc')
|
const strategy = authenticator.get<OAuth2Strategy<User>>('oidc')
|
||||||
const session = await sessionStorage.getSession(request.headers.get('cookie'))
|
const session = await sessionStorage.getSession(request.headers.get('cookie'))
|
||||||
const requestId = context.get(requestIdContext)
|
const requestId = context.get(requestIdContext)
|
||||||
|
const now = new Date().toISOString()
|
||||||
|
|
||||||
let user = session.get('user')
|
let user = session.get('user')
|
||||||
|
|
||||||
if (!user) {
|
session.set('returnTo', new URL(request.url).toString())
|
||||||
console.log(`[${requestId}] There is no user logged in`)
|
|
||||||
|
|
||||||
session.set('returnTo', new URL(request.url).toString())
|
if (!user) {
|
||||||
|
console.log(`[${now}][${requestId}] There is no user logged in`)
|
||||||
|
|
||||||
return redirect('/login', {
|
return redirect('/login', {
|
||||||
headers: new Headers({
|
headers: new Headers({
|
||||||
@@ -44,16 +46,13 @@ export const authMiddleware = async (
|
|||||||
refreshToken: tokens.refreshToken()
|
refreshToken: tokens.refreshToken()
|
||||||
}
|
}
|
||||||
|
|
||||||
console.debug(
|
console.debug(`[${now}] [${requestId}] Refresh token retrieved`, user)
|
||||||
`[${new Date().toISOString()}] [${requestId}] Refresh token retrieved`,
|
|
||||||
user
|
|
||||||
)
|
|
||||||
// Should replace the user in the session
|
// Should replace the user in the session
|
||||||
session.set('user', user)
|
session.set('user', user)
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
console.error(`[${requestId}]`, error?.stack)
|
console.error(`[${now}] [${requestId}]`, error?.stack)
|
||||||
|
|
||||||
// If refreshing the token fails, remove the user from the current session
|
// If refreshing the token fails, remove the user from the current session
|
||||||
// so the user is forced to sign in again
|
// so the user is forced to sign in again
|
||||||
|
|||||||
Reference in New Issue
Block a user