Reformating auth a bit
This commit is contained in:
@@ -1,39 +1,34 @@
|
||||
import { AuthProvider } from "@refinedev/core";
|
||||
|
||||
const API_URL = "/api/v1";
|
||||
const LOCAL_STORAGE_USER_KEY = "rpk-gui-current-user"
|
||||
const LOCAL_STORAGE_USER_KEY = "rpk-gui-current-user";
|
||||
const GOOGLE_SCOPES = { "scopes": "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email" };
|
||||
const DISCORD_SCOPES = { "scopes": "identify email" }
|
||||
|
||||
export const authProvider: AuthProvider = {
|
||||
login: async ({ providerName, email, password }) => {
|
||||
const to_param = findGetParameter("to");
|
||||
if (providerName) {
|
||||
let params, url = "";
|
||||
let scope = {};
|
||||
if (providerName === "google") {
|
||||
params = new URLSearchParams(
|
||||
{"scopes":
|
||||
"https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email"
|
||||
}
|
||||
);
|
||||
url = API_URL + "/auth/google/authorize?" + params.toString();
|
||||
scope = GOOGLE_SCOPES;
|
||||
} else if (providerName === "discord") {
|
||||
params = new URLSearchParams({"scopes": "identify email"});
|
||||
url = API_URL + "/auth/discord/authorize?" + params.toString();
|
||||
scope = DISCORD_SCOPES;
|
||||
}
|
||||
const response = await fetch(url, {method: "GET", },);
|
||||
|
||||
const params = new URLSearchParams(scope);
|
||||
const url = `${API_URL}/auth/${providerName}/authorize?${params.toString()}`;
|
||||
const response = await fetch(url, { method: "GET", },);
|
||||
const body = await response.json();
|
||||
|
||||
if (to_param) {
|
||||
localStorage.setItem("redirect_after_login", to_param);
|
||||
}
|
||||
console.log(body.authorization_url);
|
||||
window.location.href = body.authorization_url;
|
||||
return { success: true }
|
||||
}
|
||||
else if (email !== undefined && password !== undefined) {
|
||||
return { success: true };
|
||||
} else if (email !== undefined && password !== undefined) {
|
||||
const params = new URLSearchParams({"grant_type": "password", "username": email, "password": password});
|
||||
const response = await fetch(
|
||||
API_URL + "/auth/login",
|
||||
`${API_URL}/auth/login`,
|
||||
{
|
||||
method: "POST",
|
||||
body: params.toString(),
|
||||
@@ -41,7 +36,7 @@ export const authProvider: AuthProvider = {
|
||||
},
|
||||
);
|
||||
if (response.status >= 200 && response.status < 300) {
|
||||
const response = await fetch(API_URL + "/users/me");
|
||||
const response = await fetch(`${API_URL}/users/me`);
|
||||
const user = await response.json();
|
||||
store_user(user);
|
||||
|
||||
@@ -52,7 +47,7 @@ export const authProvider: AuthProvider = {
|
||||
return { success: false };
|
||||
},
|
||||
logout: async () => {
|
||||
const response = await fetch(API_URL + "/auth/logout", { method: "POST" });
|
||||
const response = await fetch(`${API_URL}/auth/logout`, { method: "POST" });
|
||||
if (response.status == 204 || response.status == 401) {
|
||||
forget_user();
|
||||
return { success: true };
|
||||
@@ -68,7 +63,7 @@ export const authProvider: AuthProvider = {
|
||||
return user;
|
||||
}
|
||||
|
||||
const response = await fetch(API_URL + "/users/me");
|
||||
const response = await fetch(`${API_URL}/users/me`);
|
||||
if (response.status < 200 || response.status > 299) {
|
||||
return
|
||||
}
|
||||
@@ -78,7 +73,7 @@ export const authProvider: AuthProvider = {
|
||||
return user_data;
|
||||
},
|
||||
register: async (params) => {
|
||||
const response = await fetch(API_URL + "/register", {
|
||||
const response = await fetch(`${API_URL}/register`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(params),
|
||||
headers: {
|
||||
@@ -87,10 +82,7 @@ export const authProvider: AuthProvider = {
|
||||
});
|
||||
|
||||
if (response.status == 201) {
|
||||
return {
|
||||
success: true,
|
||||
redirectTo: "/",
|
||||
};
|
||||
return { success: true, redirectTo: "/", };
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -102,7 +94,7 @@ export const authProvider: AuthProvider = {
|
||||
};
|
||||
},
|
||||
forgotPassword: async (params) => {
|
||||
const response = await fetch(API_URL + "/users/forgot-password", {
|
||||
const response = await fetch(`${API_URL}/users/forgot-password`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(params),
|
||||
headers: {
|
||||
@@ -111,19 +103,14 @@ export const authProvider: AuthProvider = {
|
||||
});
|
||||
|
||||
if (response.status == 202) {
|
||||
return {
|
||||
success: true,
|
||||
redirectTo: "/",
|
||||
};
|
||||
return { success: true, redirectTo: "/", };
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
return { success: false, };
|
||||
},
|
||||
updatePassword: async (params) => {
|
||||
if (params.token !== undefined) {
|
||||
const response = await fetch(API_URL + "/users/reset-password", {
|
||||
const response = await fetch(`${API_URL}/users/reset-password`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
password: params.password,
|
||||
@@ -135,16 +122,11 @@ export const authProvider: AuthProvider = {
|
||||
});
|
||||
|
||||
if (response.status == 200) {
|
||||
return {
|
||||
success: true,
|
||||
redirectTo: "/",
|
||||
};
|
||||
return { success: true, redirectTo: "/", };
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
return { success: false, };
|
||||
},
|
||||
getPermissions: async () => { throw new Error("Not implemented"); },
|
||||
onError: async (error) => {
|
||||
|
||||
Reference in New Issue
Block a user