trpc: get user from auth api
This commit is contained in:
parent
9696ced8c2
commit
2249be74f5
@ -1,11 +1,9 @@
|
||||
import { t } from './server';
|
||||
import { getUser } from '@/api/user/query';
|
||||
import getServerConfig from '@/config/server';
|
||||
import { TRPCError } from '@trpc/server';
|
||||
import jwtDecode from 'jwt-decode';
|
||||
|
||||
type TokenPayload = {
|
||||
domain: string;
|
||||
username: string;
|
||||
};
|
||||
const { COOKIE_TOKEN_NAME } = getServerConfig();
|
||||
|
||||
export const user = t.middleware(async ({ ctx, next }) => {
|
||||
if (ctx.token === undefined || ctx.token === null || typeof ctx.token !== 'string') {
|
||||
@ -14,15 +12,13 @@ export const user = t.middleware(async ({ ctx, next }) => {
|
||||
});
|
||||
}
|
||||
|
||||
const decoded = jwtDecode<TokenPayload>(ctx.token);
|
||||
const domainName = `${decoded.domain}\\${decoded.username}`;
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
user: {
|
||||
...decoded,
|
||||
domainName,
|
||||
},
|
||||
user: await getUser({
|
||||
headers: {
|
||||
Cookie: `${COOKIE_TOKEN_NAME}=${ctx.token}`,
|
||||
},
|
||||
}),
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user