@codeleap/auth 4.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +23 -0
- package/package.json.bak +23 -0
- package/src/AuthErrors/data.ts +23 -0
- package/src/AuthErrors/index.ts +123 -0
- package/src/AuthErrors/types.ts +10 -0
- package/src/index.ts +1 -0
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@codeleap/auth",
|
|
3
|
+
"version": "4.3.0",
|
|
4
|
+
"main": "src/index.ts",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"repository": {
|
|
7
|
+
"url": "https://github.com/codeleap-uk/internal-libs-monorepo.git",
|
|
8
|
+
"type": "git",
|
|
9
|
+
"directory": "packages/auth"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@codeleap/config": "4.3.0",
|
|
13
|
+
"@codeleap/types": "4.3.0",
|
|
14
|
+
"ts-node-dev": "1.1.8"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "echo 'No build needed'"
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"@codeleap/types": "4.3.0",
|
|
21
|
+
"typescript": "5.0.4"
|
|
22
|
+
}
|
|
23
|
+
}
|
package/package.json.bak
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@codeleap/auth",
|
|
3
|
+
"version": "4.3.0",
|
|
4
|
+
"main": "src/index.ts",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"repository": {
|
|
7
|
+
"url": "https://github.com/codeleap-uk/internal-libs-monorepo.git",
|
|
8
|
+
"type": "git",
|
|
9
|
+
"directory": "packages/auth"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@codeleap/config": "workspace:*",
|
|
13
|
+
"@codeleap/types": "workspace:*",
|
|
14
|
+
"ts-node-dev": "1.1.8"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "echo 'No build needed'"
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"@codeleap/types": "workspace:*",
|
|
21
|
+
"typescript": "5.0.4"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
|
|
2
|
+
export const DEFAULT_AUTH_ERRORS = {
|
|
3
|
+
'auth/wrong-password': 'Email or password is incorrect',
|
|
4
|
+
'auth/not-registered': 'This user is not registered',
|
|
5
|
+
'auth/requires-recent-login': 'You need to log in again to continue',
|
|
6
|
+
'auth/invalid-login-credentials': 'Email or password is incorrect',
|
|
7
|
+
'auth/too-many-requests': 'Access to this account has been temporarily disabled due to many failed login attempts.',
|
|
8
|
+
'auth/email-in-use': 'This email address is already taken',
|
|
9
|
+
'auth/email-not-found': 'Could not find an account matching the specified email address',
|
|
10
|
+
'auth/email-already-in-use': 'This email address is already taken',
|
|
11
|
+
'auth/invalid-email': 'Email or password is incorrect',
|
|
12
|
+
'auth/user-disabled': 'Could not find an account with the specified email address and password',
|
|
13
|
+
'auth/user-not-found': 'Email or password is incorrect',
|
|
14
|
+
'auth/missing-email': 'Could not find an account with the specified email address and password',
|
|
15
|
+
|
|
16
|
+
'social': {
|
|
17
|
+
'12501': null,
|
|
18
|
+
'EUNSPECIFIED': null,
|
|
19
|
+
'facebook-login-canceled': null,
|
|
20
|
+
'-5': null,
|
|
21
|
+
'1001': null,
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { AnyFunction } from '@codeleap/types'
|
|
2
|
+
import { DEFAULT_AUTH_ERRORS } from './data'
|
|
3
|
+
import { DefaultAuthError, Err, TAuthError } from './types'
|
|
4
|
+
|
|
5
|
+
export * from './types'
|
|
6
|
+
|
|
7
|
+
class AuthError extends Error {
|
|
8
|
+
code: TAuthError['code']
|
|
9
|
+
msg: TAuthError['msg']
|
|
10
|
+
|
|
11
|
+
constructor(message: TAuthError['msg'], code: TAuthError['code']) {
|
|
12
|
+
const _error = 'AuthError:' + code?.toLocaleUpperCase()
|
|
13
|
+
super(_error)
|
|
14
|
+
this.name = _error
|
|
15
|
+
this.code = code
|
|
16
|
+
this.msg = message
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type ErrorHandler<E> = (err: E | null, module?: string, args?: any) => void
|
|
21
|
+
|
|
22
|
+
export class AppAuthErrors<T extends Record<string, string | object | AnyFunction>, F extends ErrorHandler<AuthError>> {
|
|
23
|
+
public errors: Omit<Record<keyof T | keyof typeof DEFAULT_AUTH_ERRORS, AuthError>, 'social'>
|
|
24
|
+
|
|
25
|
+
public social: Omit<Record<keyof T['social'] | keyof typeof DEFAULT_AUTH_ERRORS['social'], AuthError>, 'social'>
|
|
26
|
+
|
|
27
|
+
public onError: ErrorHandler<Err>
|
|
28
|
+
|
|
29
|
+
public defaultErrors = DEFAULT_AUTH_ERRORS
|
|
30
|
+
|
|
31
|
+
private socialKey: string = 'social'
|
|
32
|
+
|
|
33
|
+
constructor(newErrors: T, errorFunction: F) {
|
|
34
|
+
const defaultErrors = DEFAULT_AUTH_ERRORS
|
|
35
|
+
|
|
36
|
+
const socialAuthErrors = {
|
|
37
|
+
...defaultErrors[this.socialKey],
|
|
38
|
+
...(newErrors?.[this.socialKey] as object ?? {}),
|
|
39
|
+
} as any
|
|
40
|
+
|
|
41
|
+
const socialErrors = this.registryErrors(socialAuthErrors)
|
|
42
|
+
this.social = socialErrors
|
|
43
|
+
|
|
44
|
+
if (!!newErrors['social']) {
|
|
45
|
+
delete newErrors['social']
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (!!defaultErrors['social']) {
|
|
49
|
+
delete defaultErrors['social']
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const baseAuthErrors = {
|
|
53
|
+
...defaultErrors,
|
|
54
|
+
...(newErrors as object ?? {}),
|
|
55
|
+
} as any
|
|
56
|
+
|
|
57
|
+
const authErrors = this.registryErrors(baseAuthErrors)
|
|
58
|
+
this.errors = authErrors
|
|
59
|
+
|
|
60
|
+
this.registryErrorFunction(errorFunction)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public getError(err: Err): AuthError | null {
|
|
64
|
+
if (!err) return null
|
|
65
|
+
|
|
66
|
+
if (typeof err == 'string') {
|
|
67
|
+
return this.errors?.[err] ?? this.social?.[err]
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const authError = err as TAuthError
|
|
71
|
+
|
|
72
|
+
if (!!authError?.code) {
|
|
73
|
+
return this.errors?.[authError?.code] ?? this.social?.[authError?.code]
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return null
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public isError(err: Err, key: Exclude<keyof T | DefaultAuthError, 'social'>) {
|
|
80
|
+
const _error = this.errors?.[key]
|
|
81
|
+
|
|
82
|
+
return err == _error.code || (err as TAuthError)?.code == _error.code
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public verifyError(err: Err): boolean {
|
|
86
|
+
return !!(this.getError(err))
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public exception(err: Err): void | AuthError {
|
|
90
|
+
const _error = this.getError(err)
|
|
91
|
+
|
|
92
|
+
if (_error != null) {
|
|
93
|
+
throw new AuthError(_error?.msg, _error?.code)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public createAuthError(message: TAuthError['msg'], code: TAuthError['code']) {
|
|
98
|
+
return new AuthError(message, code)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private registryErrors(unregisteredErrors: T): any {
|
|
102
|
+
const state = {}
|
|
103
|
+
|
|
104
|
+
for (const errorKey in (unregisteredErrors as object)) {
|
|
105
|
+
const errorMsg = unregisteredErrors[errorKey] as string
|
|
106
|
+
|
|
107
|
+
if (typeof errorMsg === 'string' || errorMsg == null) {
|
|
108
|
+
state[errorKey] = new AuthError(() => errorMsg, errorKey)
|
|
109
|
+
} else if (typeof errorMsg === 'function') {
|
|
110
|
+
state[errorKey] = new AuthError(errorMsg, errorKey)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return state
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private registryErrorFunction(pureErrorFunction: Function) {
|
|
118
|
+
this.onError = (err, module = null, args = {}) => {
|
|
119
|
+
const authError = this.getError(err)
|
|
120
|
+
pureErrorFunction?.(authError, module, args)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './AuthErrors'
|