forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
107 lines (97 loc) · 2.65 KB
/
auth.ts
File metadata and controls
107 lines (97 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { signIn as signInApi, signUp as signUpApi } from '@slowreader/api'
import { customAlphabet } from 'nanoid'
import { getClient } from './client.ts'
import { getEnvironment } from './environment.ts'
import { checkErrors } from './lib/http.ts'
import { encryptionKey, hasPassword, syncServer, userId } from './settings.ts'
let generateUserId = customAlphabet('0123456789', 16)
let generateKey = customAlphabet(
'abcdefghijklmnopqrstuvwxyz' +
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'0123456789' +
'!@#$%^&*()-_+=/?<>"\';:[]{}',
10
)
export interface Credentials {
encryptionKey: string
password: string
userId: string
}
function useServer(domain: string | undefined): string {
if (domain) {
syncServer.set(domain)
} else {
let server = getEnvironment().server
if (typeof server !== 'string') {
return ''
/* node:coverage ignore next 3 */
} else {
domain = server
}
}
let protocol = domain.startsWith('localhost') ? 'http' : 'https'
return `${protocol}://${domain}`
}
export async function signIn(
credentials: Credentials,
server?: string
): Promise<void> {
let host = useServer(server)
let response = await checkErrors(
signInApi,
{
password: credentials.password,
userId: credentials.userId
},
host
)
getEnvironment().saveSession(response.session)
hasPassword.set(true)
useCredentials(credentials)
}
export async function signUp(
credentials: Credentials,
server?: string
): Promise<void> {
let host = useServer(server)
let response = await checkErrors(
signUpApi,
{ password: credentials.password, userId: credentials.userId },
host
)
getEnvironment().saveSession(response.session)
hasPassword.set(true)
useCredentials(credentials)
}
export function generateCredentials(user?: string, key?: string): Credentials {
return {
encryptionKey: key ?? generateKey(),
password: generateKey(),
userId: user ?? generateUserId()
}
}
/**
* Generate string combining server’s password and local encryption key
* to use it in password managers.
*/
export function toSecret(credentials: Credentials): string {
return `${credentials.password} ${credentials.encryptionKey}`
}
/**
* Start app locally using this user ID.
*/
export function useCredentials(credentials: Credentials): void {
encryptionKey.set(credentials.encryptionKey)
userId.set(credentials.userId)
}
export async function signOut(): Promise<void> {
await getClient().clean()
userId.set(undefined)
hasPassword.set(false)
encryptionKey.set(undefined)
syncServer.set(undefined)
let env = getEnvironment()
env.saveSession(undefined)
env.cleanStorage()
env.restartApp()
}