mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: migreate get user profile to trpc
This commit is contained in:
1 parent
ac92950177
commit
423548804a
11 files changed
+54
-62
No files matched your search
+13
-2
@@ -1,12 +1,13 @@
|
||||
import { initTRPC } from '@trpc/server'
|
||||
import { initTRPC, TRPCError } from '@trpc/server'
|
||||
import { CreateNextContextOptions } from '@trpc/server/adapters/next'
|
||||
|
||||
export const createContext = async (opts: CreateNextContextOptions) => {
|
||||
const { session, user, ip } = opts.req
|
||||
const { session, user, ip, isAuthenticated } = opts.req
|
||||
return {
|
||||
session,
|
||||
user,
|
||||
ip,
|
||||
isAuthenticated,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,3 +19,13 @@ const t = initTRPC.context<AppContext>().create()
|
||||
* that can be used throughout the router
|
||||
*/
|
||||
export const { router, procedure: publicProcedure } = t
|
||||
|
||||
export const protectedProcedure = publicProcedure.use(async (opts) => {
|
||||
if (opts.ctx.isAuthenticated() && !('newUser' in opts.ctx.user)) {
|
||||
return opts.next()
|
||||
}
|
||||
throw new TRPCError({
|
||||
code: 'UNAUTHORIZED',
|
||||
message: 'Login required',
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user