Created
July 18, 2022 10:53
-
-
Save beerose/80f37b4b36cbd7ba2745701959e3cb8b to your computer and use it in GitHub Desktop.
Blitz.js auth + Redis example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import IoRedis from 'ioredis'; | |
import { setupBlitz } from '@blitzjs/next'; | |
import { AuthServerPlugin, simpleRolesIsAuthorized, SessionModel, Session } from '@blitzjs/auth'; | |
const dbs: Record<string, IoRedis.Redis | undefined> = { | |
default: undefined, | |
auth: undefined | |
}; | |
export function getRedis(): IoRedis.Redis { | |
if (dbs.default) { | |
return dbs.default; | |
} | |
return (dbs.default = createRedis(0)); | |
} | |
export function getAuthRedis(): IoRedis.Redis { | |
if (dbs.auth) { | |
return dbs.auth; | |
} | |
return (dbs.auth = createRedis(1)); | |
} | |
export function createRedis(db: number) { | |
return new IoRedis({ | |
port: 6379, | |
host: 'localhost', | |
keepAlive: 60, | |
keyPrefix: 'auth:', | |
db | |
}); | |
} | |
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 }); | |
} | |
}); | |
}); | |
}); | |
}, | |
getSession(handle: string): Promise<SessionModel | null> { | |
return new Promise<SessionModel | null>((resolve, reject) => { | |
getAuthRedis() | |
.get(`token:${handle}`) | |
.then((data: string | null) => { | |
if (data) { | |
resolve(JSON.parse(data)); | |
} else { | |
resolve(null); | |
} | |
}) | |
.catch(reject); | |
}); | |
}, | |
getSessions(userId: Session.PublicData['userId']): Promise<SessionModel[]> { | |
return new Promise<SessionModel[]>((resolve, reject) => { | |
getAuthRedis() | |
.lrange(`device:${String(userId)}`, 0, -1) | |
.then((result) => { | |
if (result) { | |
resolve( | |
result.map((handle) => { | |
return this.getSession(handle); | |
}) | |
); | |
} else { | |
resolve([]); | |
} | |
}) | |
.catch(reject); | |
}); | |
}, | |
updateSession(handle: string, session: Partial<SessionModel>): Promise<SessionModel> { | |
return new Promise<SessionModel>((resolve, reject) => { | |
getAuthRedis().get(`token:${handle}`).then((result) => { | |
if (result) { | |
const oldSession = JSON.parse(result) as SessionModel; | |
const merge = Object.assign(oldSession, session); | |
getAuthRedis().set(`token:${handle}`, JSON.stringify(merge)).catch(reject); | |
} | |
reject(new Error('cant update session')); | |
}); | |
}); | |
} | |
} | |
}) | |
] | |
}); | |
export { gSSP, gSP, api }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, I've just created a new version with Redis TTL Feature that I think will help manage applications on bigger scales.