33 lines
703 B
TypeScript
33 lines
703 B
TypeScript
import crypto from 'node:crypto';
|
|
|
|
export type WechatAuthStateRecord = {
|
|
state: string;
|
|
redirectPath: string;
|
|
createdAt: string;
|
|
};
|
|
|
|
export class WechatAuthStateStore {
|
|
private readonly states = new Map<string, WechatAuthStateRecord>();
|
|
|
|
create(redirectPath: string) {
|
|
const state = crypto.randomBytes(18).toString('hex');
|
|
const record: WechatAuthStateRecord = {
|
|
state,
|
|
redirectPath,
|
|
createdAt: new Date().toISOString(),
|
|
};
|
|
this.states.set(state, record);
|
|
return record;
|
|
}
|
|
|
|
consume(state: string) {
|
|
const record = this.states.get(state) ?? null;
|
|
if (!record) {
|
|
return null;
|
|
}
|
|
|
|
this.states.delete(state);
|
|
return record;
|
|
}
|
|
}
|