Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | 20x 63x 62x 62x 62x 62x 62x 62x 62x 13x 13x 13x 13x 13x 62x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 62x 1x 1x 1x 1x 1x 1x 1x 62x 1x 1x 1x 62x 14x 14x 13x 9x 9x 9x 9x 9x 9x 9x 4x 4x 3x 3x 3x 3x 3x 1x 1x 1x 1x 62x 2x 2x 1x 1x 1x 1x 62x 2x 2x 1x 1x 1x 1x 62x 2x 2x 2x 1x 1x 62x 8x 54x 32x 22x 20x 20x | import React, { useCallback, useEffect, useRef, useState } from 'react'; import PropTypes from 'prop-types'; import AuthContext from './AuthContext'; import { client } from '../../API'; import Loader from '../../Components/Loader'; const Auth = props => { const [coreError, setCoreError] = useState(null); const [user, setUser] = useState(null); const [authenticated, setAuthenticated] = useState(null); const [preferences, setPreferences] = useState({}); const isMountedRef = useRef(true); const isMounted = useCallback(() => isMountedRef.current, []); let controller = new AbortController(); useEffect(() => { /* istanbul ignore else */ if (props.checkOnInit) { checkAuthentication(); } return () => { void (isMountedRef.current = false); controller.abort(); } }, []); const signIn = async (username, password) => { return new Promise(async (resolve, reject) => { try { // Get CSRF Cookie await client('/sanctum/csrf_cookie', undefined, { accept: 'text/html', signal: controller.signal }); // Perform sign in await client('/login', { username, password }, { signal: controller.signal }); // When correct, get the user object const { data } = await client('/api/user', undefined, { signal: controller.signal }); let _preferences = data.preferences ? /* istanbul ignore next */ JSON.parse(data.preferences) : {}; delete data.preferences; /* istanbul ignore else */ if (isMounted()) { setUser(data); setAuthenticated(true); setPreferences(_preferences); return resolve(data); } } catch (error) { /* istanbul ignore else */ if (!error.status?.isAbort && isMounted()) { return reject(error); } } }); } const signOut = async () => { new Promise(async (resolve, reject) => { try { await client('/api/logout', {}) /* istanbul ignore else */ if (isMounted()) { setUser(null); setAuthenticated(false); // Only if we are not in a test environment (Jest) /* istanbul ignore next */ if (process.env.JEST_WORKER_ID === undefined || process.env.NODE_ENV !== 'test') { window.location.replace(window.location.origin + '/login?logout'); } resolve(true); } } catch (error) { /* istanbul ignore next */ if (!error.status?.isAbort && isMounted()) { return reject(error); } } }); } const _setUser = (_user, _authenticated) => { /* istanbul ignore else */ if (isMounted()) { setUser(_user); setAuthenticated(_authenticated); } } const checkAuthentication = async () => { return new Promise(async (resolve, reject) => { if (authenticated === null) { // The status is null if it hasn't been checked await client('/api/user', undefined, { signal: controller.signal }) .then(json => { let _preferences = json.data.preferences ? /* istanbul ignore next */ JSON.parse(json.data.preferences) : {}; delete json.data.preferences; /* istanbul ignore else */ if (isMounted()) { setUser(json.data) setAuthenticated(true); setPreferences(_preferences); return resolve(true); } }) .catch(error => { /* istanbul ignore else */ if (!error.status?.isAbort) { if (error.response && error.status.code === 401) { // If 401 returns, the user is not logged in /* istanbul ignore else */ if (isMounted()) { setUser(null) setAuthenticated(false); setPreferences({}); return resolve(false); } } else { // Any other code, something went wrong /* istanbul ignore else */ if (isMounted()) { setCoreError(error.data?.message); } } } }); } else { // We've already check authenticated, just return state /* istanbul ignore else */ if (isMounted()) { return resolve(authenticated); } } }); } const checkPermission = async permission => { return new Promise(async (resolve, reject) => { await client('/api/permission/check', { 'permission': permission }, { signal: controller.signal }) .then(json => { /* istanbul ignore else */ if (isMounted()) { return resolve(json.data.has_permission); } }) .catch(error => { /* istanbul ignore else */ if (!error.status?.isAbort && isMounted()) { setCoreError(error.data?.message); } }) }); } const checkGroup = async group => { return new Promise(async (resolve, reject) => { await client('/api/group/check', { 'group': group }, { signal: controller.signal }) .then(json => { /* istanbul ignore else */ if (isMounted()) { return resolve(json.data.in_group); } }) .catch(error => { /* istanbul ignore else */ if (!error.status?.isAbort && isMounted()) { setCoreError(error.data?.message); } }) }); } const setPreference = async (preference, value) => { preferences[preference] = value; setPreferences({ ...preferences }); await client('/api/user/preference', { 'preference': preference, 'value': value }, { method: 'PUT', signal: controller.signal }) .catch(error => { /* istanbul ignore else */ if (!error.status?.isAbort && isMounted()) { setCoreError(error.data?.message); } }) } if (coreError) { throw Error(coreError); } if (authenticated !== null) { return ( <AuthContext.Provider children={props.children} value={{ user, authenticated, signIn, signOut, setUser: _setUser, checkAuthentication, checkPermission, checkGroup, preferences, setPreference }} /> ) } else { return <Loader /> } } Auth.propTypes = { config: PropTypes.object, checkOnInit: PropTypes.bool }; Auth.defaultProps = { checkOnInit: true } export default Auth; |