-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuseAuthentication.js
176 lines (159 loc) · 4.95 KB
/
useAuthentication.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import React, {
useCallback, useMemo, useEffect, useState,
} from 'react';
import PropTypes from 'prop-types';
import deepFreeze from 'deep-freeze';
import {
authenticate,
defaultErrorMessages,
parseError,
ERROR_SERVER_UNREACHABLE,
ERROR_NETWORK_DISCONNECTED,
} from '../../core';
import { LoginForm } from '.';
import { useDeepCompareCallback, useDeepCompareEffect, useDeepCompareMemo } from 'use-deep-compare';
function useAuthentication({
messages,
authentication: _authentication,
onAuthentication,
config,
loadAuthentication,
saveAuthentication,
onError,
}) {
const authentication = _authentication && deepFreeze(_authentication);
const [error, setError] = useState();
if (!messages) {
messages = defaultErrorMessages;
}
const logout = useCallback((_auth) => {
saveAuthentication && saveAuthentication();
}, [saveAuthentication]);
/*
const update = useCallback(async (_auth) => {
if (_auth && _auth.remember && saveAuthentication) {
await saveAuthentication(_auth);
}
return onAuthentication && onAuthentication(_auth);
}, [onAuthentication]);
*/
const update = useCallback((_auth) => {
if (_auth) {
if (saveAuthentication) {
if (_auth.remember) {
saveAuthentication(_auth);
}
else {
saveAuthentication();
}
}
}
if (onAuthentication) {
onAuthentication(_auth);
}
}, [onAuthentication, saveAuthentication]);
useDeepCompareEffect(() => {
if (!authentication && loadAuthentication) {
loadAuthentication().then(_authentication => {
if (_authentication) {
update(_authentication);
}
});
}
}, [authentication, loadAuthentication, update]);
const onSubmitLogin = useDeepCompareCallback(async ({
username, password, remember,
}) => {
try {
const authentication = await authenticate({
username, password, config,
});
authentication.remember = remember;
if (authentication) {
const { user, token } = authentication;
if (user && token) {
setError();
update(authentication);
} else {
if (!user) {
setError(messages.usernameError);
} else if (!token) {
setError(messages.passwordError);
}
}
} else {
console.log('authentication failed?', authentication);
}
} catch (e) {
console.log('Authentication error:', e);
const friendlyError = parseError(e);
setError(friendlyError.errorMessage);
onError && onError(friendlyError)
}
}, [
config, logout, update,
messages.genericError, messages.networkError, messages.passwordError, messages.serverError, messages.usernameError,
]);
const onSubmit = useDeepCompareCallback(async ({
username, password, remember,
}) => {
if (authentication) {
logout();
update();
} else {
await onSubmitLogin({ username, password, remember });
}
}, [authentication, config, logout, update, onSubmitLogin]);
const component = useDeepCompareMemo(() => (
config && (
<LoginForm
config={config}
authentication={authentication}
actionText={messages.actionText}
errorText={error}
onSubmit={onSubmit}
/>
)
), [authentication, config, error, messages.actionText, onSubmit]);
const _config = (authentication && authentication.config) || config;
const response = {
state: authentication,
actions: { update, logout, onLoginFormSubmit: onSubmit, onLoginFormSubmitLogin: onSubmitLogin },
component,
config: _config,
messages: messages,
};
return response;
};
useAuthentication.propTypes = {
/** Pass a previously returned authentication object to bypass login. */
authentication: PropTypes.shape({
user: PropTypes.object.isRequired,
token: PropTypes.object.isRequired,
config: PropTypes.object.isRequired,
remember: PropTypes.bool,
}),
/** Callback function to propogate the user/token used for API Authentication. */
onAuthentication: PropTypes.func,
/** Configuration to pass through to the Authentication component. */
/** Override the default text and errors. Must override all or none. */
messages: PropTypes.shape({
actionText: PropTypes.string.isRequired,
genericError: PropTypes.string.isRequired,
usernameError: PropTypes.string.isRequired,
passwordError: PropTypes.string.isRequired,
}),
config: PropTypes.shape({
/** The Gitea server to use when authenticating. */
server: PropTypes.string.isRequired,
/** The id of the token to create/retrieve that is used for the app. */
tokenid: PropTypes.string.isRequired,
}),
/** Callback function to persist authentication. */
saveAuthentication: PropTypes.func,
/** Callback function to retrieve persisted authentication. */
loadAuthentication: PropTypes.func,
/** Callback function on error. */
onError: PropTypes.func,
};
export default useAuthentication;