invalid client id gmgngngngngng ntm
This commit is contained in:
parent
d8a650bdd3
commit
c3e9ffcb6d
3 changed files with 92 additions and 16 deletions
|
|
@ -7,15 +7,17 @@ import * as redirect from "./redirect";
|
|||
*/
|
||||
async function getToken(code) {
|
||||
|
||||
const endpoint = `${VITE_AUTH_URL}/...`;
|
||||
const auth_url = import.meta.env.VITE_URL;
|
||||
const endpoint = `${auth_url}/auth-api/token`;
|
||||
const formData = {
|
||||
"grant_type": "authorization_code",
|
||||
"code": code,
|
||||
"redirect_uri": redirect.createLink(),
|
||||
"client_id": process.env.VITE_CLIENT_ID
|
||||
"client_id": import.meta.env.VITE_CLIENT_ID
|
||||
}
|
||||
const request = {
|
||||
method: "POST",
|
||||
// mode: 'cors',
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
|
|
@ -23,10 +25,17 @@ async function getToken(code) {
|
|||
body: new URLSearchParams(formData)
|
||||
};
|
||||
|
||||
const response = await fetch(endpoint, request)
|
||||
if (!response.ok) {
|
||||
try {
|
||||
const response = await fetch(endpoint, request)
|
||||
if (!response.ok) {
|
||||
throw new Error(response.statusText)
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
console.error("Failed to retrieve OIDC token")
|
||||
console.debug(response)
|
||||
alert("Failed to retrieve OIDC token")
|
||||
|
||||
console.error(err)
|
||||
|
||||
localStorage.clear();
|
||||
redirect.redirectToLoginPage()
|
||||
|
|
@ -41,9 +50,10 @@ async function getToken(code) {
|
|||
id_token
|
||||
} = response.body
|
||||
|
||||
// console.debug("Saving token and refresh tokens...")
|
||||
localStorage.setItem("token", id_token);
|
||||
localStorage.setItem("refresh_token", refresh_token);
|
||||
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
@ -51,10 +61,56 @@ async function getToken(code) {
|
|||
/**
|
||||
* @param {string} refreshToken the refresh token (optional)
|
||||
* @returns {boolean} whether the token has been refreshed or not
|
||||
* @warn NOT IMPLEMENTED
|
||||
*/
|
||||
// TODO
|
||||
// async function refreshToken(refreshToken) {}
|
||||
async function refreshToken(refreshToken) {
|
||||
|
||||
const auth_url = import.meta.env.VITE_URL;
|
||||
const endpoint = `${auth_url}/auth-api/token`;
|
||||
const formData = {
|
||||
client_id: import.meta.env.VITE_CLIENT_ID,
|
||||
client_secret: "...",
|
||||
grant_type: "code",
|
||||
refresh_token: refreshToken,
|
||||
scope: "epita profile picture",
|
||||
}
|
||||
const request = {
|
||||
method: "POST",
|
||||
// mode: 'cors',
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
|
||||
body: new URLSearchParams(formData)
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch(endpoint, request)
|
||||
if (!response.ok) {
|
||||
throw new Error(response.statusText)
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
console.error("Failed to retrieve OIDC token")
|
||||
|
||||
localStorage.clear();
|
||||
redirect.redirectToLoginPage()
|
||||
return false
|
||||
}
|
||||
|
||||
const {
|
||||
// access_token,
|
||||
// token_type,
|
||||
refresh_token,
|
||||
// expires_in,
|
||||
id_token
|
||||
} = response.body
|
||||
|
||||
// console.debug("Saving token and refresh tokens...")
|
||||
localStorage.setItem("token", id_token);
|
||||
localStorage.setItem("refresh_token", refresh_token);
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {boolean} true if the user is authenticated, false otherwise
|
||||
|
|
@ -75,6 +131,8 @@ async function authenticate() {
|
|||
// missing functions:
|
||||
// - authedAPIRequest (makes an authenticated request to the API)
|
||||
|
||||
module.exports = {
|
||||
|
||||
export {
|
||||
getToken,
|
||||
// refreshToken,
|
||||
authenticate
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue