-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
166 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,61 @@ | ||
import { AuthProvider } from "react-admin"; | ||
|
||
const AUTH_API_URL = "/api/authentication"; | ||
export const PREFIX = "scrapp-lab"; | ||
|
||
const authProvider: AuthProvider = { | ||
login: async ({ username, password }) => { | ||
const request = new Request(`${AUTH_API_URL}/login`, { | ||
method: "POST", | ||
body: JSON.stringify({ email: username, password }), | ||
credentials: "include", | ||
headers: new Headers({ "Content-Type": "application/json" }), | ||
}); | ||
const response = await fetch(request); | ||
if (response.status < 200 || response.status >= 300) { | ||
throw new Error("Utilisateur ou mot de passe incorrect"); | ||
} | ||
const auth = await response.json(); | ||
localStorage.setItem(`${PREFIX}-auth`, JSON.stringify(auth)); | ||
}, | ||
logout: () => { | ||
localStorage.removeItem(`${PREFIX}-auth`); | ||
return Promise.resolve(); | ||
}, | ||
checkAuth: () => { | ||
return localStorage.getItem(`${PREFIX}-auth`) | ||
? Promise.resolve() | ||
: Promise.reject(); | ||
}, | ||
checkError: (error) => { | ||
if (error?.status === 401) { | ||
const authProviderFactory: (endpoint: string | undefined) => AuthProvider = ( | ||
endpoint: string | undefined | ||
) => { | ||
const AUTH_API_URL = `${endpoint || "/api"}/authentication`; | ||
return { | ||
login: async ({ username, password }) => { | ||
const request = new Request(`${AUTH_API_URL}/login`, { | ||
method: "POST", | ||
body: JSON.stringify({ email: username, password }), | ||
credentials: "include", | ||
headers: new Headers({ "Content-Type": "application/json" }), | ||
}); | ||
const response = await fetch(request); | ||
if (response.status < 200 || response.status >= 300) { | ||
throw new Error("Utilisateur ou mot de passe incorrect"); | ||
} | ||
const auth = await response.json(); | ||
localStorage.setItem(`${PREFIX}-auth`, JSON.stringify(auth)); | ||
}, | ||
logout: () => { | ||
localStorage.removeItem(`${PREFIX}-auth`); | ||
return Promise.reject(); | ||
} | ||
return Promise.resolve(); | ||
}, | ||
getIdentity: async () => { | ||
const auth = JSON.parse(localStorage.getItem(`${PREFIX}-auth`) || "null"); | ||
if (!auth) { | ||
return Promise.reject({ | ||
status: 401, | ||
return Promise.resolve(); | ||
}, | ||
checkAuth: () => { | ||
return localStorage.getItem(`${PREFIX}-auth`) | ||
? Promise.resolve() | ||
: Promise.reject(); | ||
}, | ||
checkError: (error) => { | ||
if (error?.status === 401) { | ||
localStorage.removeItem(`${PREFIX}-auth`); | ||
return Promise.reject(); | ||
} | ||
return Promise.resolve(); | ||
}, | ||
getIdentity: async () => { | ||
const auth = JSON.parse(localStorage.getItem(`${PREFIX}-auth`) || "null"); | ||
if (!auth) { | ||
return Promise.reject({ | ||
status: 401, | ||
}); | ||
} | ||
return Promise.resolve({ | ||
...auth, | ||
fullName: `${auth.firstName} ${auth.lastName}`, | ||
}); | ||
} | ||
return Promise.resolve({ | ||
...auth, | ||
fullName: `${auth.firstName} ${auth.lastName}`, | ||
}); | ||
}, | ||
getPermissions: () => { | ||
const auth = JSON.parse(localStorage.getItem(`${PREFIX}-auth`) || "null"); | ||
if (!auth) { | ||
return Promise.resolve([]); | ||
} | ||
return Promise.resolve(auth.permissions); | ||
}, | ||
}, | ||
getPermissions: () => { | ||
const auth = JSON.parse(localStorage.getItem(`${PREFIX}-auth`) || "null"); | ||
if (!auth) { | ||
return Promise.resolve([]); | ||
} | ||
return Promise.resolve(auth.permissions); | ||
}, | ||
}; | ||
}; | ||
|
||
export default authProvider; | ||
export default authProviderFactory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.