@redmix/auth 0.0.1
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/LICENSE +21 -0
- package/README.md +148 -0
- package/dist/AuthContext.d.ts +69 -0
- package/dist/AuthContext.d.ts.map +1 -0
- package/dist/AuthContext.js +7 -0
- package/dist/AuthImplementation.d.ts +49 -0
- package/dist/AuthImplementation.d.ts.map +1 -0
- package/dist/AuthImplementation.js +0 -0
- package/dist/AuthProvider/AuthProvider.d.ts +12 -0
- package/dist/AuthProvider/AuthProvider.d.ts.map +1 -0
- package/dist/AuthProvider/AuthProvider.js +83 -0
- package/dist/AuthProvider/AuthProviderState.d.ts +13 -0
- package/dist/AuthProvider/AuthProviderState.d.ts.map +1 -0
- package/dist/AuthProvider/AuthProviderState.js +18 -0
- package/dist/AuthProvider/ServerAuthProvider.d.ts +23 -0
- package/dist/AuthProvider/ServerAuthProvider.d.ts.map +1 -0
- package/dist/AuthProvider/ServerAuthProvider.js +43 -0
- package/dist/AuthProvider/useCurrentUser.d.ts +3 -0
- package/dist/AuthProvider/useCurrentUser.d.ts.map +1 -0
- package/dist/AuthProvider/useCurrentUser.js +31 -0
- package/dist/AuthProvider/useForgotPassword.d.ts +3 -0
- package/dist/AuthProvider/useForgotPassword.d.ts.map +1 -0
- package/dist/AuthProvider/useForgotPassword.js +18 -0
- package/dist/AuthProvider/useHasRole.d.ts +3 -0
- package/dist/AuthProvider/useHasRole.d.ts.map +1 -0
- package/dist/AuthProvider/useHasRole.js +34 -0
- package/dist/AuthProvider/useLogIn.d.ts +5 -0
- package/dist/AuthProvider/useLogIn.d.ts.map +1 -0
- package/dist/AuthProvider/useLogIn.js +22 -0
- package/dist/AuthProvider/useLogOut.d.ts +4 -0
- package/dist/AuthProvider/useLogOut.d.ts.map +1 -0
- package/dist/AuthProvider/useLogOut.js +21 -0
- package/dist/AuthProvider/useReauthenticate.d.ts +5 -0
- package/dist/AuthProvider/useReauthenticate.d.ts.map +1 -0
- package/dist/AuthProvider/useReauthenticate.js +57 -0
- package/dist/AuthProvider/useResetPassword.d.ts +3 -0
- package/dist/AuthProvider/useResetPassword.d.ts.map +1 -0
- package/dist/AuthProvider/useResetPassword.js +18 -0
- package/dist/AuthProvider/useSignUp.d.ts +5 -0
- package/dist/AuthProvider/useSignUp.d.ts.map +1 -0
- package/dist/AuthProvider/useSignUp.js +20 -0
- package/dist/AuthProvider/useToken.d.ts +3 -0
- package/dist/AuthProvider/useToken.d.ts.map +1 -0
- package/dist/AuthProvider/useToken.js +15 -0
- package/dist/AuthProvider/useValidateResetToken.d.ts +3 -0
- package/dist/AuthProvider/useValidateResetToken.d.ts.map +1 -0
- package/dist/AuthProvider/useValidateResetToken.js +18 -0
- package/dist/authFactory.d.ts +15 -0
- package/dist/authFactory.d.ts.map +1 -0
- package/dist/authFactory.js +16 -0
- package/dist/cjs/AuthContext.d.ts +69 -0
- package/dist/cjs/AuthContext.d.ts.map +1 -0
- package/dist/cjs/AuthContext.js +41 -0
- package/dist/cjs/AuthImplementation.d.ts +49 -0
- package/dist/cjs/AuthImplementation.d.ts.map +1 -0
- package/dist/cjs/AuthImplementation.js +16 -0
- package/dist/cjs/AuthProvider/AuthProvider.d.ts +12 -0
- package/dist/cjs/AuthProvider/AuthProvider.d.ts.map +1 -0
- package/dist/cjs/AuthProvider/AuthProvider.js +117 -0
- package/dist/cjs/AuthProvider/AuthProviderState.d.ts +13 -0
- package/dist/cjs/AuthProvider/AuthProviderState.d.ts.map +1 -0
- package/dist/cjs/AuthProvider/AuthProviderState.js +43 -0
- package/dist/cjs/AuthProvider/ServerAuthProvider.d.ts +23 -0
- package/dist/cjs/AuthProvider/ServerAuthProvider.d.ts.map +1 -0
- package/dist/cjs/AuthProvider/ServerAuthProvider.js +78 -0
- package/dist/cjs/AuthProvider/useCurrentUser.d.ts +3 -0
- package/dist/cjs/AuthProvider/useCurrentUser.d.ts.map +1 -0
- package/dist/cjs/AuthProvider/useCurrentUser.js +55 -0
- package/dist/cjs/AuthProvider/useForgotPassword.d.ts +3 -0
- package/dist/cjs/AuthProvider/useForgotPassword.d.ts.map +1 -0
- package/dist/cjs/AuthProvider/useForgotPassword.js +42 -0
- package/dist/cjs/AuthProvider/useHasRole.d.ts +3 -0
- package/dist/cjs/AuthProvider/useHasRole.d.ts.map +1 -0
- package/dist/cjs/AuthProvider/useHasRole.js +58 -0
- package/dist/cjs/AuthProvider/useLogIn.d.ts +5 -0
- package/dist/cjs/AuthProvider/useLogIn.d.ts.map +1 -0
- package/dist/cjs/AuthProvider/useLogIn.js +46 -0
- package/dist/cjs/AuthProvider/useLogOut.d.ts +4 -0
- package/dist/cjs/AuthProvider/useLogOut.d.ts.map +1 -0
- package/dist/cjs/AuthProvider/useLogOut.js +45 -0
- package/dist/cjs/AuthProvider/useReauthenticate.d.ts +5 -0
- package/dist/cjs/AuthProvider/useReauthenticate.d.ts.map +1 -0
- package/dist/cjs/AuthProvider/useReauthenticate.js +81 -0
- package/dist/cjs/AuthProvider/useResetPassword.d.ts +3 -0
- package/dist/cjs/AuthProvider/useResetPassword.d.ts.map +1 -0
- package/dist/cjs/AuthProvider/useResetPassword.js +42 -0
- package/dist/cjs/AuthProvider/useSignUp.d.ts +5 -0
- package/dist/cjs/AuthProvider/useSignUp.d.ts.map +1 -0
- package/dist/cjs/AuthProvider/useSignUp.js +44 -0
- package/dist/cjs/AuthProvider/useToken.d.ts +3 -0
- package/dist/cjs/AuthProvider/useToken.d.ts.map +1 -0
- package/dist/cjs/AuthProvider/useToken.js +39 -0
- package/dist/cjs/AuthProvider/useValidateResetToken.d.ts +3 -0
- package/dist/cjs/AuthProvider/useValidateResetToken.d.ts.map +1 -0
- package/dist/cjs/AuthProvider/useValidateResetToken.js +42 -0
- package/dist/cjs/authFactory.d.ts +15 -0
- package/dist/cjs/authFactory.d.ts.map +1 -0
- package/dist/cjs/authFactory.js +40 -0
- package/dist/cjs/getCurrentUserFromMiddleware.d.ts +2 -0
- package/dist/cjs/getCurrentUserFromMiddleware.d.ts.map +1 -0
- package/dist/cjs/getCurrentUserFromMiddleware.js +47 -0
- package/dist/cjs/index.d.ts +11 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +39 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/useAuth.d.ts +6 -0
- package/dist/cjs/useAuth.d.ts.map +1 -0
- package/dist/cjs/useAuth.js +78 -0
- package/dist/getCurrentUserFromMiddleware.d.ts +2 -0
- package/dist/getCurrentUserFromMiddleware.d.ts.map +1 -0
- package/dist/getCurrentUserFromMiddleware.js +23 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/package.json +1 -0
- package/dist/useAuth.d.ts +6 -0
- package/dist/useAuth.d.ts.map +1 -0
- package/dist/useAuth.js +43 -0
- package/package.json +110 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Redmix
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
# Authentication
|
2
|
+
|
3
|
+
## Contributing
|
4
|
+
|
5
|
+
If you want to contribute a new auth provider integration we recommend you
|
6
|
+
start by implementing it as a custom auth provider in a Redwood App first. When
|
7
|
+
that works you can package it up as an npm package and publish it on your own.
|
8
|
+
You can then create a PR on this repo with support for your new auth provider
|
9
|
+
in our `yarn rw setup auth` cli command. The easiest option is probably to just
|
10
|
+
look at one of the existing auth providers in
|
11
|
+
`packages/cli/src/commands/setup/auth/providers` and the corresponding
|
12
|
+
templates in `../templates`.
|
13
|
+
|
14
|
+
If you need help setting up a custom auth provider you can read the auth docs
|
15
|
+
on the web.
|
16
|
+
|
17
|
+
### Contributing to the base auth implementation
|
18
|
+
|
19
|
+
If you want to contribute to our auth implementation, the interface towards
|
20
|
+
both auth service providers and RW apps we recommend you start looking in
|
21
|
+
`authFactory.ts` and then continue to `AuthProvider.tsx`. `AuthProvider.tsx`
|
22
|
+
has most of our implementation together with all the custom hooks it uses.
|
23
|
+
Another file to be accustomed with is `AuthContext.ts`. The interface in there
|
24
|
+
has pretty good code comments, and is what will be exposed to RW apps.
|
25
|
+
|
26
|
+
## getCurrentUser
|
27
|
+
|
28
|
+
`getCurrentUser` returns the user information together with
|
29
|
+
an optional collection of roles used by requireAuth() to check if the user is authenticated or has role-based access.
|
30
|
+
|
31
|
+
Use in conjunction with `requireAuth` in your services to check that a user is logged in, whether or not they are assigned a role, and optionally raise an error if they're not.
|
32
|
+
|
33
|
+
```js
|
34
|
+
@param decoded - The decoded access token containing user info and JWT claims like `sub`
|
35
|
+
@param { token, SupportedAuthTypes type } - The access token itself as well as the auth provider type
|
36
|
+
@param { APIGatewayEvent event, Context context } - An object which contains information from the invoker
|
37
|
+
such as headers and cookies, and the context information about the invocation such as IP Address
|
38
|
+
```
|
39
|
+
|
40
|
+
### Examples
|
41
|
+
|
42
|
+
#### Checks if currentUser is authenticated
|
43
|
+
|
44
|
+
This example is the standard use of `getCurrentUser`.
|
45
|
+
|
46
|
+
```js
|
47
|
+
export const getCurrentUser = async (
|
48
|
+
decoded,
|
49
|
+
{ _token, _type },
|
50
|
+
{ _event, _context },
|
51
|
+
) => {
|
52
|
+
return { ...decoded, roles: parseJWT({ decoded }).roles }
|
53
|
+
}
|
54
|
+
```
|
55
|
+
|
56
|
+
#### User details fetched via database query
|
57
|
+
|
58
|
+
```js
|
59
|
+
export const getCurrentUser = async (decoded) => {
|
60
|
+
return await db.user.findUnique({ where: { decoded.email } })
|
61
|
+
}
|
62
|
+
```
|
63
|
+
|
64
|
+
#### User info is decoded from the access token
|
65
|
+
|
66
|
+
```js
|
67
|
+
export const getCurrentUser = async (decoded) => {
|
68
|
+
return { ...decoded }
|
69
|
+
}
|
70
|
+
```
|
71
|
+
|
72
|
+
#### User info is contained in the decoded token and roles extracted
|
73
|
+
|
74
|
+
```js
|
75
|
+
export const getCurrentUser = async (decoded) => {
|
76
|
+
return { ...decoded, roles: parseJWT({ decoded }).roles }
|
77
|
+
}
|
78
|
+
```
|
79
|
+
|
80
|
+
#### User record query by email with namespaced app_metadata roles as Auth0 requires custom JWT claims to be namespaced
|
81
|
+
|
82
|
+
```js
|
83
|
+
export const getCurrentUser = async (decoded) => {
|
84
|
+
const currentUser = await db.user.findUnique({
|
85
|
+
where: { email: decoded.email },
|
86
|
+
})
|
87
|
+
|
88
|
+
return {
|
89
|
+
...currentUser,
|
90
|
+
roles: parseJWT({ decoded: decoded, namespace: NAMESPACE }).roles,
|
91
|
+
}
|
92
|
+
}
|
93
|
+
```
|
94
|
+
|
95
|
+
#### User record query by an identity with app_metadata roles
|
96
|
+
|
97
|
+
```js
|
98
|
+
const getCurrentUser = async (decoded) => {
|
99
|
+
const currentUser = await db.user.findUnique({
|
100
|
+
where: { userIdentity: decoded.sub },
|
101
|
+
})
|
102
|
+
return {
|
103
|
+
...currentUser,
|
104
|
+
roles: parseJWT({ decoded: decoded }).roles,
|
105
|
+
}
|
106
|
+
}
|
107
|
+
```
|
108
|
+
|
109
|
+
#### Cookies and other request information are available in the req parameter, just in case
|
110
|
+
|
111
|
+
```js
|
112
|
+
const getCurrentUser = async (_decoded, _raw, { event, _context }) => {
|
113
|
+
const cookies = cookie(event.headers.cookies)
|
114
|
+
const session = cookies['my.cookie.name']
|
115
|
+
const currentUser = await db.sessions.findUnique({ where: { id: session } })
|
116
|
+
return currentUser
|
117
|
+
}
|
118
|
+
```
|
119
|
+
|
120
|
+
## requireAuth
|
121
|
+
|
122
|
+
Use `requireAuth` in your services to check that a user is logged in, whether or not they are assigned a role, and optionally raise an error if they're not.
|
123
|
+
|
124
|
+
```js
|
125
|
+
@param {string=} roles - An optional role or list of roles
|
126
|
+
@param {string[]=} roles - An optional list of roles
|
127
|
+
|
128
|
+
@returns {boolean} - If the currentUser is authenticated (and assigned one of the given roles)
|
129
|
+
|
130
|
+
@throws {AuthenticationError} - If the currentUser is not authenticated
|
131
|
+
@throws {ForbiddenError} If the currentUser is not allowed due to role permissions
|
132
|
+
```
|
133
|
+
|
134
|
+
### Examples
|
135
|
+
|
136
|
+
#### Checks if currentUser is authenticated
|
137
|
+
|
138
|
+
```js
|
139
|
+
requireAuth()
|
140
|
+
```
|
141
|
+
|
142
|
+
#### Checks if currentUser is authenticated and assigned one of the given roles
|
143
|
+
|
144
|
+
```js
|
145
|
+
requireAuth({ role: 'admin' })
|
146
|
+
requireAuth({ role: ['editor', 'author'] })
|
147
|
+
requireAuth({ role: ['publisher'] })
|
148
|
+
```
|
@@ -0,0 +1,69 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
export interface CurrentUser {
|
3
|
+
[key: string]: unknown;
|
4
|
+
}
|
5
|
+
export interface AuthContextInterface<TUser, TLogInOptions, TLogIn, TLogOutOptions, TLogOut, TSignUpOptions, TSignUp, TForgotPassword, TResetPasswordOptions, TResetPassword, TValidateResetToken, TClient> {
|
6
|
+
/** Determining your current authentication state */
|
7
|
+
loading: boolean;
|
8
|
+
isAuthenticated: boolean;
|
9
|
+
/** The current user's data from the `getCurrentUser` function on the api side */
|
10
|
+
currentUser: null | CurrentUser;
|
11
|
+
/**
|
12
|
+
* The user's metadata from the auth service provider
|
13
|
+
*
|
14
|
+
* Exactly what this looks like will depend on the auth service provider,
|
15
|
+
* but one example is this
|
16
|
+
* ```json
|
17
|
+
* {
|
18
|
+
* "id": "11111111-2222-3333-4444-5555555555555",
|
19
|
+
* "aud": "authenticated",
|
20
|
+
* "role": "authenticated",
|
21
|
+
* "email": "email@example.com",
|
22
|
+
* "app_metadata": {
|
23
|
+
* "provider": "email"
|
24
|
+
* },
|
25
|
+
* "user_metadata": null,
|
26
|
+
* "created_at": "2016-05-15T19:53:12.368652374-07:00",
|
27
|
+
* "updated_at": "2016-05-15T19:53:12.368652374-07:00"
|
28
|
+
* }
|
29
|
+
* ```
|
30
|
+
*/
|
31
|
+
userMetadata: null | TUser;
|
32
|
+
logIn(options?: TLogInOptions): Promise<TLogIn>;
|
33
|
+
logOut(options?: TLogOutOptions): Promise<TLogOut>;
|
34
|
+
signUp(options?: TSignUpOptions): Promise<TSignUp>;
|
35
|
+
/**
|
36
|
+
* Clients should always return null or string
|
37
|
+
* It is expected that they catch any errors internally
|
38
|
+
*/
|
39
|
+
getToken(): Promise<null | string>;
|
40
|
+
/**
|
41
|
+
* Fetches the "currentUser" from the api side,
|
42
|
+
* but does not update the current user state.
|
43
|
+
*/
|
44
|
+
getCurrentUser(): Promise<null | CurrentUser>;
|
45
|
+
/**
|
46
|
+
* Checks if the "currentUser" from the api side
|
47
|
+
* is assigned a role or one of a list of roles.
|
48
|
+
* If the user is assigned any of the provided list of roles,
|
49
|
+
* the hasRole is considered to be true.
|
50
|
+
**/
|
51
|
+
hasRole(rolesToCheck: string | string[]): boolean;
|
52
|
+
/**
|
53
|
+
* Redetermine authentication state and update the state.
|
54
|
+
*/
|
55
|
+
reauthenticate(): Promise<void>;
|
56
|
+
forgotPassword(username: string): Promise<TForgotPassword>;
|
57
|
+
resetPassword(options?: TResetPasswordOptions): Promise<TResetPassword>;
|
58
|
+
validateResetToken(resetToken: string | null): Promise<TValidateResetToken>;
|
59
|
+
/**
|
60
|
+
* A reference to auth service provider sdk "client", which is useful if we
|
61
|
+
* do not support some specific functionality.
|
62
|
+
*/
|
63
|
+
client?: TClient;
|
64
|
+
type: string;
|
65
|
+
hasError: boolean;
|
66
|
+
error?: Error;
|
67
|
+
}
|
68
|
+
export declare function createAuthContext<TUser, TLogInOptions, TLogIn, TLogOutOptions, TLogOut, TSignUpOptions, TSignUp, TForgotPassword, TResetPasswordOptions, TResetPassword, TValidateResetToken, TClient>(): React.Context<AuthContextInterface<TUser, TLogInOptions, TLogIn, TLogOutOptions, TLogOut, TSignUpOptions, TSignUp, TForgotPassword, TResetPasswordOptions, TResetPassword, TValidateResetToken, TClient> | undefined>;
|
69
|
+
//# sourceMappingURL=AuthContext.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"AuthContext.d.ts","sourceRoot":"","sources":["../src/AuthContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,MAAM,WAAW,WAAW;IAC1B,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CACvB;AAED,MAAM,WAAW,oBAAoB,CACnC,KAAK,EACL,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO;IAEP,oDAAoD;IACpD,OAAO,EAAE,OAAO,CAAA;IAChB,eAAe,EAAE,OAAO,CAAA;IACxB,iFAAiF;IACjF,WAAW,EAAE,IAAI,GAAG,WAAW,CAAA;IAC/B;;;;;;;;;;;;;;;;;;;OAmBG;IACH,YAAY,EAAE,IAAI,GAAG,KAAK,CAAA;IAC1B,KAAK,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IAC/C,MAAM,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAClD,MAAM,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAClD;;;OAGG;IACH,QAAQ,IAAI,OAAO,CAAC,IAAI,GAAG,MAAM,CAAC,CAAA;IAClC;;;OAGG;IACH,cAAc,IAAI,OAAO,CAAC,IAAI,GAAG,WAAW,CAAC,CAAA;IAC7C;;;;;QAKI;IACJ,OAAO,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO,CAAA;IACjD;;OAEG;IACH,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;IAC/B,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,CAAA;IAC1D,aAAa,CAAC,OAAO,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,cAAc,CAAC,CAAA;IACvE,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAA;IAC3E;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,OAAO,CAAA;IACjB,KAAK,CAAC,EAAE,KAAK,CAAA;CACd;AAED,wBAAgB,iBAAiB,CAC/B,KAAK,EACL,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,2NAmBR"}
|
@@ -0,0 +1,49 @@
|
|
1
|
+
export interface AuthImplementation<TUser = unknown, TRestoreAuth = unknown, TLogInOptions = unknown, TLogIn = unknown, TLogOutOptions = unknown, TLogOut = unknown, TSignUpOptions = unknown, TSignUp = unknown, TForgotPassword = unknown, TResetPasswordOptions = unknown, TResetPassword = unknown, TValidateResetToken = unknown, TClient = unknown> {
|
2
|
+
type: string;
|
3
|
+
client?: TClient;
|
4
|
+
restoreAuthState?(): Promise<TRestoreAuth>;
|
5
|
+
login(options?: TLogInOptions): Promise<TLogIn>;
|
6
|
+
logout(options?: TLogOutOptions): Promise<TLogOut>;
|
7
|
+
signup(options?: TSignUpOptions): Promise<TSignUp>;
|
8
|
+
getToken(): Promise<string | null>;
|
9
|
+
forgotPassword?(username: string): Promise<TForgotPassword>;
|
10
|
+
resetPassword?(options?: TResetPasswordOptions): Promise<TResetPassword>;
|
11
|
+
validateResetToken?(token: string | null): Promise<TValidateResetToken>;
|
12
|
+
clientHasLoaded?(): boolean;
|
13
|
+
/**
|
14
|
+
* The user's data from the AuthProvider
|
15
|
+
*
|
16
|
+
* Exactly what this looks like will depend on the auth service provider,
|
17
|
+
* but one example is this
|
18
|
+
* ```json
|
19
|
+
* {
|
20
|
+
* "id": "11111111-2222-3333-4444-5555555555555",
|
21
|
+
* "aud": "authenticated",
|
22
|
+
* "role": "authenticated",
|
23
|
+
* "email": "email@example.com",
|
24
|
+
* "app_metadata": {
|
25
|
+
* "provider": "email"
|
26
|
+
* },
|
27
|
+
* "user_metadata": null,
|
28
|
+
* "created_at": "2016-05-15T19:53:12.368652374-07:00",
|
29
|
+
* "updated_at": "2016-05-15T19:53:12.368652374-07:00"
|
30
|
+
* }
|
31
|
+
* ```
|
32
|
+
*/
|
33
|
+
getUserMetadata(): Promise<TUser | null>;
|
34
|
+
/**
|
35
|
+
* Set "loading" to true while the auth provider is reauthenticating.
|
36
|
+
*/
|
37
|
+
loadWhileReauthenticating?: boolean;
|
38
|
+
/**
|
39
|
+
* This property is either manually set by the user, or inferred from the
|
40
|
+
* experimental.streamingSsr setting in TOML
|
41
|
+
*/
|
42
|
+
middlewareAuthEnabled?: boolean;
|
43
|
+
/**
|
44
|
+
* This is the endpoint on the middleware we are going to hit for POST
|
45
|
+
* requests
|
46
|
+
*/
|
47
|
+
getAuthUrl?: () => string;
|
48
|
+
}
|
49
|
+
//# sourceMappingURL=AuthImplementation.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"AuthImplementation.d.ts","sourceRoot":"","sources":["../src/AuthImplementation.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,kBAAkB,CACjC,KAAK,GAAG,OAAO,EACf,YAAY,GAAG,OAAO,EACtB,aAAa,GAAG,OAAO,EACvB,MAAM,GAAG,OAAO,EAChB,cAAc,GAAG,OAAO,EACxB,OAAO,GAAG,OAAO,EACjB,cAAc,GAAG,OAAO,EACxB,OAAO,GAAG,OAAO,EACjB,eAAe,GAAG,OAAO,EACzB,qBAAqB,GAAG,OAAO,EAC/B,cAAc,GAAG,OAAO,EACxB,mBAAmB,GAAG,OAAO,EAC7B,OAAO,GAAG,OAAO;IAEjB,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,CAAC,EAAE,OAAO,CAAA;IAEhB,gBAAgB,CAAC,IAAI,OAAO,CAAC,YAAY,CAAC,CAAA;IAC1C,KAAK,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IAC/C,MAAM,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAClD,MAAM,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAClD,QAAQ,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAA;IAClC,cAAc,CAAC,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,CAAA;IAC3D,aAAa,CAAC,CAAC,OAAO,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,cAAc,CAAC,CAAA;IACxE,kBAAkB,CAAC,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAA;IACvE,eAAe,CAAC,IAAI,OAAO,CAAA;IAE3B;;;;;;;;;;;;;;;;;;;OAmBG;IACH,eAAe,IAAI,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC,CAAA;IAExC;;OAEG;IACH,yBAAyB,CAAC,EAAE,OAAO,CAAA;IAEnC;;;OAGG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAA;IAC/B;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,MAAM,CAAA;CAC1B"}
|
File without changes
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import type { ReactNode } from 'react';
|
2
|
+
import React from 'react';
|
3
|
+
import type { AuthContextInterface, CurrentUser } from '../AuthContext.js';
|
4
|
+
import type { AuthImplementation } from '../AuthImplementation.js';
|
5
|
+
export interface AuthProviderProps {
|
6
|
+
children: ReactNode;
|
7
|
+
}
|
8
|
+
export declare function createAuthProvider<TUser, TRestoreAuth, TLogInOptions, TLogIn, TLogOutOptions, TLogOut, TSignUpOptions, TSignUp, TForgotPassword, TResetPasswordOptions, TResetPassword, TValidateResetToken, TClient>(AuthContext: React.Context<AuthContextInterface<TUser, TLogInOptions, TLogIn, TLogOutOptions, TLogOut, TSignUpOptions, TSignUp, TForgotPassword, TResetPasswordOptions, TResetPassword, TValidateResetToken, TClient> | undefined>, authImplementation: AuthImplementation<TUser, TRestoreAuth, TLogInOptions, TLogIn, TLogOutOptions, TLogOut, TSignUpOptions, TSignUp, TForgotPassword, TResetPasswordOptions, TResetPassword, TValidateResetToken, TClient>, customProviderHooks?: {
|
9
|
+
useCurrentUser?: () => Promise<CurrentUser>;
|
10
|
+
useHasRole?: (currentUser: CurrentUser | null) => (rolesToCheck: string | string[]) => boolean;
|
11
|
+
}): ({ children }: AuthProviderProps) => React.JSX.Element;
|
12
|
+
//# sourceMappingURL=AuthProvider.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"AuthProvider.d.ts","sourceRoot":"","sources":["../../src/AuthProvider/AuthProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AACtC,OAAO,KAA0C,MAAM,OAAO,CAAA;AAE9D,OAAO,KAAK,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC1E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAgBlE,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,SAAS,CAAA;CACpB;AAED,wBAAgB,kBAAkB,CAChC,KAAK,EACL,YAAY,EACZ,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,EAEP,WAAW,EAAE,KAAK,CAAC,OAAO,CACtB,oBAAoB,CAClB,KAAK,EACL,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,CACR,GACD,SAAS,CACZ,EACD,kBAAkB,EAAE,kBAAkB,CACpC,KAAK,EACL,YAAY,EACZ,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,CACR,EACD,mBAAmB,CAAC,EAAE;IACpB,cAAc,CAAC,EAAE,MAAM,OAAO,CAAC,WAAW,CAAC,CAAA;IAC3C,UAAU,CAAC,EAAE,CACX,WAAW,EAAE,WAAW,GAAG,IAAI,KAC5B,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,EAAE,KAAK,OAAO,CAAA;CAClD,kBAEmC,iBAAiB,uBA4FtD"}
|
@@ -0,0 +1,83 @@
|
|
1
|
+
import React, { useContext, useEffect, useState } from "react";
|
2
|
+
import { spaDefaultAuthProviderState } from "./AuthProviderState.js";
|
3
|
+
import { ServerAuthContext } from "./ServerAuthProvider.js";
|
4
|
+
import { useCurrentUser } from "./useCurrentUser.js";
|
5
|
+
import { useForgotPassword } from "./useForgotPassword.js";
|
6
|
+
import { useHasRole } from "./useHasRole.js";
|
7
|
+
import { useLogIn } from "./useLogIn.js";
|
8
|
+
import { useLogOut } from "./useLogOut.js";
|
9
|
+
import { useReauthenticate } from "./useReauthenticate.js";
|
10
|
+
import { useResetPassword } from "./useResetPassword.js";
|
11
|
+
import { useSignUp } from "./useSignUp.js";
|
12
|
+
import { useToken } from "./useToken.js";
|
13
|
+
import { useValidateResetToken } from "./useValidateResetToken.js";
|
14
|
+
function createAuthProvider(AuthContext, authImplementation, customProviderHooks) {
|
15
|
+
const AuthProvider = ({ children }) => {
|
16
|
+
const serverAuthState = useContext(ServerAuthContext);
|
17
|
+
const [authProviderState, setAuthProviderState] = useState(serverAuthState || spaDefaultAuthProviderState);
|
18
|
+
const getToken = useToken(authImplementation);
|
19
|
+
const getCurrentUser = customProviderHooks?.useCurrentUser ? customProviderHooks.useCurrentUser : (
|
20
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
21
|
+
useCurrentUser(authImplementation)
|
22
|
+
);
|
23
|
+
const reauthenticate = useReauthenticate(
|
24
|
+
authImplementation,
|
25
|
+
setAuthProviderState,
|
26
|
+
getCurrentUser
|
27
|
+
);
|
28
|
+
const hasRole = customProviderHooks?.useHasRole ? customProviderHooks.useHasRole(authProviderState.currentUser) : (
|
29
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
30
|
+
useHasRole(authProviderState.currentUser)
|
31
|
+
);
|
32
|
+
const signUp = useSignUp(
|
33
|
+
authImplementation,
|
34
|
+
setAuthProviderState,
|
35
|
+
getCurrentUser
|
36
|
+
);
|
37
|
+
const logIn = useLogIn(
|
38
|
+
authImplementation,
|
39
|
+
setAuthProviderState,
|
40
|
+
getCurrentUser
|
41
|
+
);
|
42
|
+
const logOut = useLogOut(authImplementation, setAuthProviderState);
|
43
|
+
const forgotPassword = useForgotPassword(authImplementation);
|
44
|
+
const resetPassword = useResetPassword(authImplementation);
|
45
|
+
const validateResetToken = useValidateResetToken(authImplementation);
|
46
|
+
const type = authImplementation.type;
|
47
|
+
const client = authImplementation.client;
|
48
|
+
useEffect(() => {
|
49
|
+
async function doRestoreState() {
|
50
|
+
await authImplementation.restoreAuthState?.();
|
51
|
+
if (!serverAuthState) {
|
52
|
+
reauthenticate();
|
53
|
+
}
|
54
|
+
}
|
55
|
+
doRestoreState();
|
56
|
+
}, [reauthenticate, serverAuthState]);
|
57
|
+
return /* @__PURE__ */ React.createElement(
|
58
|
+
AuthContext.Provider,
|
59
|
+
{
|
60
|
+
value: {
|
61
|
+
...authProviderState,
|
62
|
+
signUp,
|
63
|
+
logIn,
|
64
|
+
logOut,
|
65
|
+
getToken,
|
66
|
+
getCurrentUser,
|
67
|
+
hasRole,
|
68
|
+
reauthenticate,
|
69
|
+
forgotPassword,
|
70
|
+
resetPassword,
|
71
|
+
validateResetToken,
|
72
|
+
client,
|
73
|
+
type
|
74
|
+
}
|
75
|
+
},
|
76
|
+
children
|
77
|
+
);
|
78
|
+
};
|
79
|
+
return AuthProvider;
|
80
|
+
}
|
81
|
+
export {
|
82
|
+
createAuthProvider
|
83
|
+
};
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import type { CurrentUser } from '../AuthContext.js';
|
2
|
+
export type AuthProviderState<TUser, TClient = unknown> = {
|
3
|
+
loading: boolean;
|
4
|
+
isAuthenticated: boolean;
|
5
|
+
userMetadata: null | TUser;
|
6
|
+
currentUser: null | CurrentUser;
|
7
|
+
hasError: boolean;
|
8
|
+
error?: Error;
|
9
|
+
client?: TClient;
|
10
|
+
};
|
11
|
+
export declare const spaDefaultAuthProviderState: AuthProviderState<never>;
|
12
|
+
export declare const middlewareDefaultAuthProviderState: AuthProviderState<never>;
|
13
|
+
//# sourceMappingURL=AuthProviderState.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"AuthProviderState.d.ts","sourceRoot":"","sources":["../../src/AuthProvider/AuthProviderState.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAEpD,MAAM,MAAM,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,IAAI;IACxD,OAAO,EAAE,OAAO,CAAA;IAChB,eAAe,EAAE,OAAO,CAAA;IACxB,YAAY,EAAE,IAAI,GAAG,KAAK,CAAA;IAC1B,WAAW,EAAE,IAAI,GAAG,WAAW,CAAA;IAC/B,QAAQ,EAAE,OAAO,CAAA;IACjB,KAAK,CAAC,EAAE,KAAK,CAAA;IACb,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB,CAAA;AAED,eAAO,MAAM,2BAA2B,EAAE,iBAAiB,CAAC,KAAK,CAMhE,CAAA;AAED,eAAO,MAAM,kCAAkC,EAAE,iBAAiB,CAAC,KAAK,CAMvE,CAAA"}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
const spaDefaultAuthProviderState = {
|
2
|
+
loading: true,
|
3
|
+
isAuthenticated: false,
|
4
|
+
userMetadata: null,
|
5
|
+
currentUser: null,
|
6
|
+
hasError: false
|
7
|
+
};
|
8
|
+
const middlewareDefaultAuthProviderState = {
|
9
|
+
loading: false,
|
10
|
+
isAuthenticated: false,
|
11
|
+
userMetadata: null,
|
12
|
+
currentUser: null,
|
13
|
+
hasError: false
|
14
|
+
};
|
15
|
+
export {
|
16
|
+
middlewareDefaultAuthProviderState,
|
17
|
+
spaDefaultAuthProviderState
|
18
|
+
};
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import type { ReactNode } from 'react';
|
2
|
+
import React from 'react';
|
3
|
+
import type { AuthProviderState } from './AuthProviderState.js';
|
4
|
+
export type ServerAuthState = AuthProviderState<any> & {
|
5
|
+
cookieHeader: string | null | undefined;
|
6
|
+
roles: string[];
|
7
|
+
};
|
8
|
+
/**
|
9
|
+
* On the server, it resolves to the middlewareDefaultAuthProviderState first.
|
10
|
+
*
|
11
|
+
* On the client it restores from the initial server state injected in the ServerAuthProvider
|
12
|
+
*/
|
13
|
+
export declare const ServerAuthContext: React.Context<ServerAuthState>;
|
14
|
+
/**
|
15
|
+
* Note: This only gets rendered on the server and serves two purposes:
|
16
|
+
* 1) On the server, it sets the auth state
|
17
|
+
* 2) On the client, it restores the auth state from the initial server render
|
18
|
+
*/
|
19
|
+
export declare const ServerAuthProvider: ({ value, children, }: {
|
20
|
+
value: ServerAuthState;
|
21
|
+
children?: ReactNode[];
|
22
|
+
}) => React.JSX.Element;
|
23
|
+
//# sourceMappingURL=ServerAuthProvider.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"ServerAuthProvider.d.ts","sourceRoot":"","sources":["../../src/AuthProvider/ServerAuthProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AACtC,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAG/D,MAAM,MAAM,eAAe,GAAG,iBAAiB,CAAC,GAAG,CAAC,GAAG;IAGrD,YAAY,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAA;IACvC,KAAK,EAAE,MAAM,EAAE,CAAA;CAChB,CAAA;AAkBD;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,gCAE7B,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,yBAG5B;IACD,KAAK,EAAE,eAAe,CAAA;IACtB,QAAQ,CAAC,EAAE,SAAS,EAAE,CAAA;CACvB,sBAmBA,CAAA"}
|
@@ -0,0 +1,43 @@
|
|
1
|
+
import React from "react";
|
2
|
+
import { middlewareDefaultAuthProviderState } from "./AuthProviderState.js";
|
3
|
+
const getAuthInitialStateFromServer = () => {
|
4
|
+
if (globalThis?.__REDWOOD__SERVER__AUTH_STATE__) {
|
5
|
+
const initialState = {
|
6
|
+
...middlewareDefaultAuthProviderState,
|
7
|
+
encryptedSession: null,
|
8
|
+
...globalThis?.__REDWOOD__SERVER__AUTH_STATE__ || {}
|
9
|
+
};
|
10
|
+
globalThis.__REDWOOD__SERVER__AUTH_STATE__ = null;
|
11
|
+
return initialState;
|
12
|
+
}
|
13
|
+
return null;
|
14
|
+
};
|
15
|
+
const ServerAuthContext = React.createContext(
|
16
|
+
getAuthInitialStateFromServer()
|
17
|
+
);
|
18
|
+
const ServerAuthProvider = ({
|
19
|
+
value,
|
20
|
+
children
|
21
|
+
}) => {
|
22
|
+
const stringifiedAuthState = `__REDWOOD__SERVER__AUTH_STATE__ = ${JSON.stringify(
|
23
|
+
sanitizeServerAuthState(value)
|
24
|
+
)};`;
|
25
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(
|
26
|
+
"script",
|
27
|
+
{
|
28
|
+
id: "__REDWOOD__SERVER_AUTH_STATE__",
|
29
|
+
dangerouslySetInnerHTML: {
|
30
|
+
__html: stringifiedAuthState
|
31
|
+
}
|
32
|
+
}
|
33
|
+
), /* @__PURE__ */ React.createElement(ServerAuthContext.Provider, { value }, children));
|
34
|
+
};
|
35
|
+
function sanitizeServerAuthState(value) {
|
36
|
+
const sanitizedState = { ...value };
|
37
|
+
delete sanitizedState.cookieHeader;
|
38
|
+
return sanitizedState;
|
39
|
+
}
|
40
|
+
export {
|
41
|
+
ServerAuthContext,
|
42
|
+
ServerAuthProvider
|
43
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"useCurrentUser.d.ts","sourceRoot":"","sources":["../../src/AuthProvider/useCurrentUser.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAIlE,eAAO,MAAM,cAAc,uBAAwB,kBAAkB,WAGtC,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CA0B7D,CAAA"}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import { useCallback } from "react";
|
2
|
+
import { useToken } from "./useToken.js";
|
3
|
+
const useCurrentUser = (authImplementation) => {
|
4
|
+
const getToken = useToken(authImplementation);
|
5
|
+
return useCallback(async () => {
|
6
|
+
const token = await getToken();
|
7
|
+
const response = await globalThis.fetch(globalThis.RWJS_API_GRAPHQL_URL, {
|
8
|
+
method: "POST",
|
9
|
+
credentials: "include",
|
10
|
+
headers: {
|
11
|
+
"content-type": "application/json",
|
12
|
+
"auth-provider": authImplementation.type,
|
13
|
+
authorization: `Bearer ${token}`
|
14
|
+
},
|
15
|
+
body: JSON.stringify({
|
16
|
+
query: "query __REDWOOD__AUTH_GET_CURRENT_USER { redwood { currentUser } }"
|
17
|
+
})
|
18
|
+
});
|
19
|
+
if (response.ok) {
|
20
|
+
const { data } = await response.json();
|
21
|
+
return data?.redwood?.currentUser;
|
22
|
+
} else {
|
23
|
+
throw new Error(
|
24
|
+
`Could not fetch current user: ${response.statusText} (${response.status})`
|
25
|
+
);
|
26
|
+
}
|
27
|
+
}, [authImplementation, getToken]);
|
28
|
+
};
|
29
|
+
export {
|
30
|
+
useCurrentUser
|
31
|
+
};
|
@@ -0,0 +1,3 @@
|
|
1
|
+
import type { AuthImplementation } from '../AuthImplementation.js';
|
2
|
+
export declare const useForgotPassword: <TUser, TRestoreAuth, TLogInOptions, TLogIn, TLogOutOptions, TLogOut, TSignUpOptions, TSignUp, TForgotPassword, TResetPasswordOptions, TResetPassword, TValidateResetToken, TClient>(authImplementation: AuthImplementation<TUser, TRestoreAuth, TLogInOptions, TLogIn, TLogOutOptions, TLogOut, TSignUpOptions, TSignUp, TForgotPassword, TResetPasswordOptions, TResetPassword, TValidateResetToken, TClient>) => (username: string) => Promise<TForgotPassword>;
|
3
|
+
//# sourceMappingURL=useForgotPassword.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"useForgotPassword.d.ts","sourceRoot":"","sources":["../../src/AuthProvider/useForgotPassword.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAElE,eAAO,MAAM,iBAAiB,GAC5B,KAAK,EACL,YAAY,EACZ,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,sBAEa,kBAAkB,CACpC,KAAK,EACL,YAAY,EACZ,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,CACR,gBAGkB,MAAM,6BAW1B,CAAA"}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import { useCallback } from "react";
|
2
|
+
const useForgotPassword = (authImplementation) => {
|
3
|
+
return useCallback(
|
4
|
+
async (username) => {
|
5
|
+
if (authImplementation.forgotPassword) {
|
6
|
+
return await authImplementation.forgotPassword(username);
|
7
|
+
} else {
|
8
|
+
throw new Error(
|
9
|
+
`Auth client ${authImplementation.type} does not implement this function`
|
10
|
+
);
|
11
|
+
}
|
12
|
+
},
|
13
|
+
[authImplementation]
|
14
|
+
);
|
15
|
+
};
|
16
|
+
export {
|
17
|
+
useForgotPassword
|
18
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"useHasRole.d.ts","sourceRoot":"","sources":["../../src/AuthProvider/useHasRole.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAEpD,eAAO,MAAM,UAAU,gBAAiB,WAAW,GAAG,IAAI,oBAevC,MAAM,GAAG,MAAM,EAAE,KAAG,OAiCtC,CAAA"}
|
@@ -0,0 +1,34 @@
|
|
1
|
+
import { useCallback } from "react";
|
2
|
+
const useHasRole = (currentUser) => {
|
3
|
+
return useCallback(
|
4
|
+
(rolesToCheck) => {
|
5
|
+
if (currentUser?.roles) {
|
6
|
+
if (typeof rolesToCheck === "string") {
|
7
|
+
if (typeof currentUser.roles === "string") {
|
8
|
+
return currentUser.roles === rolesToCheck;
|
9
|
+
} else if (Array.isArray(currentUser.roles)) {
|
10
|
+
return currentUser.roles?.some(
|
11
|
+
(allowedRole) => rolesToCheck === allowedRole
|
12
|
+
);
|
13
|
+
}
|
14
|
+
}
|
15
|
+
if (Array.isArray(rolesToCheck)) {
|
16
|
+
if (Array.isArray(currentUser.roles)) {
|
17
|
+
return currentUser.roles?.some(
|
18
|
+
(allowedRole) => rolesToCheck.includes(allowedRole)
|
19
|
+
);
|
20
|
+
} else if (typeof currentUser.roles === "string") {
|
21
|
+
return rolesToCheck.some(
|
22
|
+
(allowedRole) => currentUser?.roles === allowedRole
|
23
|
+
);
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}
|
27
|
+
return false;
|
28
|
+
},
|
29
|
+
[currentUser]
|
30
|
+
);
|
31
|
+
};
|
32
|
+
export {
|
33
|
+
useHasRole
|
34
|
+
};
|
@@ -0,0 +1,5 @@
|
|
1
|
+
import type { AuthImplementation } from '../AuthImplementation.js';
|
2
|
+
import type { AuthProviderState } from './AuthProviderState.js';
|
3
|
+
import type { useCurrentUser } from './useCurrentUser.js';
|
4
|
+
export declare const useLogIn: <TUser, TRestoreAuth, TLogInOptions, TLogIn, TLogOutOptions, TLogOut, TSignUpOptions, TSignUp, TForgotPassword, TResetPasswordOptions, TResetPassword, TValidateResetToken, TClient>(authImplementation: AuthImplementation<TUser, TRestoreAuth, TLogInOptions, TLogIn, TLogOutOptions, TLogOut, TSignUpOptions, TSignUp, TForgotPassword, TResetPasswordOptions, TResetPassword, TValidateResetToken, TClient>, setAuthProviderState: React.Dispatch<React.SetStateAction<AuthProviderState<TUser>>>, getCurrentUser: ReturnType<typeof useCurrentUser>) => (options?: TLogInOptions) => Promise<TLogIn>;
|
5
|
+
//# sourceMappingURL=useLogIn.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"useLogIn.d.ts","sourceRoot":"","sources":["../../src/AuthProvider/useLogIn.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAElE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAE/D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAGzD,eAAO,MAAM,QAAQ,GACnB,KAAK,EACL,YAAY,EACZ,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,sBAEa,kBAAkB,CACpC,KAAK,EACL,YAAY,EACZ,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,CACR,wBACqB,KAAK,CAAC,QAAQ,CAClC,KAAK,CAAC,cAAc,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC,CAC/C,kBACe,UAAU,CAAC,OAAO,cAAc,CAAC,gBAS9B,aAAa,oBASjC,CAAA"}
|