I am trying to implement Blitz Auth in a NextJS project. I am following the docs but i am stuck here: https://blitzjs.com/docs/blitz-auth-with-next#without-prisma where i need to use prisma storage or redis storage. In this case, i am using a third party rest api. Any help on how i'd add the session storage?
#Help with Blitz Auth
1 messages · Page 1 of 1 (latest)
yeah, so you need to define all the getSession, deleteSession etc functions. Instead of redis, you'll do a fetch call to your API.
const { gSSP, gSP, api } = setupBlitz({
plugins: [
AuthServerPlugin({
cookiePrefix: "blitz-app-prefix",
isAuthorized: simpleRolesIsAuthorized,
storage: {
createSession: (session: SessionModel): Promise<SessionModel> => {
return new Promise<SessionModel>((resolve, reject) => {
getAuthRedis().set(
`token:${session.handle}`,
JSON.stringify(session),
(err) => {
if (err) {
reject(err)
} else {
getAuthRedis().lpush(
`device:${String(session.userId)}`,
session.handle
)
resolve(session)
}
}
)
})
},
deleteSession(handle: string): Promise<SessionModel> {
return new Promise<SessionModel>((resolve, reject) => {
getAuthRedis()
.get(`token:${handle}`)
.then((result) => {
if (result) {
const session = JSON.parse(result) as SessionModel
const userId = session.userId as unknown as string
getAuthRedis().lrem(userId, 0, handle).catch(reject)
}
getAuthRedis().del(handle, (err) => {
if (err) {
reject(err)
} else {
resolve({ handle })
}
})
})
})
},
here's the interface, but I just realized we don't have the SessionModel type in there
https://blitzjs.com/docs/auth-config#customize-session-persistence-and-database-access
export interface SessionModel extends Record<any, any> {
handle: string
userId?: PublicData["userId"] | null
expiresAt?: Date | null
hashedSessionToken?: string | null
antiCSRFToken?: string | null
publicData?: string | null
privateData?: string | null
}
thank you. I will give this a try
but can i use session storage?
@opaque falcon
so sorry for not getting back sooner.
Yes you can! it'll work seamlessly