🎉 first commit
This commit is contained in:
22
app/routes/api.auth.$action/check-error.server.ts
Normal file
22
app/routes/api.auth.$action/check-error.server.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import type { LoaderFunctionArgs } from '@remix-run/node';
|
||||
import { data } from '@remix-run/node';
|
||||
import { getAuthError } from '~/lib/.server/auth';
|
||||
|
||||
/**
|
||||
* 检查认证错误信息的路由
|
||||
*
|
||||
* 从会话中读取认证错误信息,并在响应中返回
|
||||
* 同时会清除错误信息,确保它只显示一次
|
||||
*/
|
||||
export async function checkErrorLoader({ request }: LoaderFunctionArgs) {
|
||||
const { errorMessage, headers } = await getAuthError(request);
|
||||
|
||||
return data(
|
||||
{
|
||||
errorMessage,
|
||||
},
|
||||
{
|
||||
headers,
|
||||
},
|
||||
);
|
||||
}
|
||||
41
app/routes/api.auth.$action/route.tsx
Normal file
41
app/routes/api.auth.$action/route.tsx
Normal file
@@ -0,0 +1,41 @@
|
||||
import type { LoaderFunctionArgs } from '@remix-run/node';
|
||||
import { logto } from '~/lib/.server/auth';
|
||||
import { checkErrorLoader } from './check-error.server';
|
||||
import { userLoader } from './user.server';
|
||||
|
||||
export const loader = async (args: LoaderFunctionArgs) => {
|
||||
const { params } = args;
|
||||
|
||||
switch (params.action) {
|
||||
case 'check-error':
|
||||
return checkErrorLoader(args);
|
||||
case 'user':
|
||||
return userLoader(args);
|
||||
default:
|
||||
/**
|
||||
* 处理认证路由
|
||||
* 支持的路由:
|
||||
* - /api/auth/sign-in - 登录
|
||||
* - /api/auth/callback - 登录回调
|
||||
* - /api/auth/sign-out - 登出
|
||||
*/
|
||||
return logto.handleAuthRoutes({
|
||||
'sign-in': {
|
||||
path: '/api/auth/sign-in',
|
||||
redirectBackTo: '/api/auth/callback',
|
||||
},
|
||||
'sign-in-callback': {
|
||||
path: '/api/auth/callback',
|
||||
redirectBackTo: '/',
|
||||
},
|
||||
'sign-out': {
|
||||
path: '/api/auth/sign-out',
|
||||
redirectBackTo: '/',
|
||||
},
|
||||
'sign-up': {
|
||||
path: '/api/auth/sign-up',
|
||||
redirectBackTo: '/api/auth/callback',
|
||||
},
|
||||
})(args);
|
||||
}
|
||||
};
|
||||
16
app/routes/api.auth.$action/user.server.ts
Normal file
16
app/routes/api.auth.$action/user.server.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { data, type LoaderFunctionArgs } from '@remix-run/node';
|
||||
import { getUser } from '~/lib/.server/auth';
|
||||
|
||||
/**
|
||||
* 用户信息API端点
|
||||
* 返回用户认证状态和用户信息
|
||||
*/
|
||||
export async function userLoader({ request }: LoaderFunctionArgs) {
|
||||
// 使用服务端 getUser 函数获取用户上下文
|
||||
const userContext = await getUser(request);
|
||||
|
||||
return data({
|
||||
isAuthenticated: userContext.isAuthenticated,
|
||||
claims: userContext.isAuthenticated ? userContext.userInfo : null,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user