Implement register and login with Telegram
Add the concept of authentication methods that authenticate an account where using the telegram login widget is one such method. If a login is done with an authentication method that's not associated with any account the session ends up with the data from the authentication method in order to allow registering a new account with the authentication method. This has to be stored on the session as otherwise it wouldn't be possible to implement authentication methods such as OAuth2 that takes the user to a third-party site and then redirects the browser back.
This commit is contained in:
parent
2d6bcebc5a
commit
aaa2faffb1
14 changed files with 357 additions and 8 deletions
|
@ -10,6 +10,9 @@ export interface ServerSession {
|
|||
id: Id,
|
||||
access: ApiUserType,
|
||||
accountId?: number,
|
||||
authenticationProvider?: "telegram",
|
||||
authenticationSlug?: string,
|
||||
authenticationName?: string,
|
||||
rotatesAtMs: number,
|
||||
expiresAtMs?: number,
|
||||
discardAtMs: number,
|
||||
|
@ -28,6 +31,14 @@ export interface ServerUser {
|
|||
locale?: string,
|
||||
}
|
||||
|
||||
export interface ServerAuthenticationMethod {
|
||||
id: Id,
|
||||
provider: "telegram",
|
||||
slug: string,
|
||||
name: string,
|
||||
userId: Id,
|
||||
}
|
||||
|
||||
// For this demo I'm just storing the runtime data in JSON files. When making
|
||||
// this into proper application this should be replaced with an actual database.
|
||||
|
||||
|
@ -37,6 +48,8 @@ const usersPath = "data/users.json";
|
|||
const nextUserIdPath = "data/next-user-id.json";
|
||||
const sessionsPath = "data/sessions.json";
|
||||
const nextSessionIdPath = "data/next-session-id.json";
|
||||
const authMethodPath = "data/auth-method.json";
|
||||
const nextAuthenticationMethodIdPath = "data/auth-method-id.json"
|
||||
|
||||
async function remove(path: string) {
|
||||
try {
|
||||
|
@ -137,3 +150,17 @@ export async function readSessions() {
|
|||
export async function writeSessions(sessions: ServerSession[]) {
|
||||
await writeFile(sessionsPath, JSON.stringify(sessions, undefined, "\t") + "\n", "utf-8");
|
||||
}
|
||||
|
||||
export async function nextAuthenticationMethodId() {
|
||||
const nextId = await readJson(nextAuthenticationMethodIdPath, 0);
|
||||
await writeFile(nextAuthenticationMethodIdPath, String(nextId + 1), "utf-8");
|
||||
return nextId;
|
||||
}
|
||||
|
||||
export async function readAuthenticationMethods() {
|
||||
return readJson<ServerAuthenticationMethod[]>(authMethodPath, [])
|
||||
}
|
||||
|
||||
export async function writeAuthenticationMethods(authMethods: ServerAuthenticationMethod[]) {
|
||||
await writeFile(authMethodPath, JSON.stringify(authMethods, undefined, "\t") + "\n", "utf-8");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue