Full authentication in front
This commit is contained in:
192
gui/rpk-gui/src/providers/auth-provider.tsx
Normal file
192
gui/rpk-gui/src/providers/auth-provider.tsx
Normal file
@@ -0,0 +1,192 @@
|
||||
import { AuthProvider } from "@refinedev/core";
|
||||
|
||||
const API_URL = "/api/v1";
|
||||
const LOCAL_STORAGE_USER_KEY = "rpk-gui-current-user"
|
||||
|
||||
export const authProvider: AuthProvider = {
|
||||
login: async ({ providerName, email, password }) => {
|
||||
const to_param = findGetParameter("to");
|
||||
if (providerName) {
|
||||
let params, url = "";
|
||||
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();
|
||||
} else if (providerName === "discord") {
|
||||
params = new URLSearchParams({"scopes": "identify email"});
|
||||
url = API_URL + "/auth/discord/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) {
|
||||
const params = new URLSearchParams({"grant_type": "password", "username": email, "password": password});
|
||||
const response = await fetch(
|
||||
API_URL + "/auth/login",
|
||||
{
|
||||
method: "POST",
|
||||
body: params.toString(),
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded", },
|
||||
},
|
||||
);
|
||||
if (response.status >= 200 && response.status < 300) {
|
||||
const response = await fetch(API_URL + "/users/me");
|
||||
const user = await response.json();
|
||||
store_user(user);
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
}
|
||||
|
||||
return { success: false };
|
||||
},
|
||||
logout: async () => {
|
||||
const response = await fetch(API_URL + "/auth/logout", { method: "POST" });
|
||||
if (response.status == 204 || response.status == 401) {
|
||||
forget_user();
|
||||
return { success: true };
|
||||
}
|
||||
return { success: false };
|
||||
},
|
||||
check: async () => {
|
||||
return { authenticated: Boolean(get_user()) };
|
||||
},
|
||||
getIdentity: async () => {
|
||||
const user = get_user();
|
||||
if (user != null) {
|
||||
return user;
|
||||
}
|
||||
|
||||
const response = await fetch(API_URL + "/users/me");
|
||||
if (response.status < 200 || response.status > 299) {
|
||||
return
|
||||
}
|
||||
const user_data = await response.json();
|
||||
store_user(user_data)
|
||||
|
||||
return user_data;
|
||||
},
|
||||
register: async (params) => {
|
||||
const response = await fetch(API_URL + "/register", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(params),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
if (response.status == 201) {
|
||||
return {
|
||||
success: true,
|
||||
redirectTo: "/",
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
error: {
|
||||
message: "Register failed",
|
||||
name: "Invalid email or password",
|
||||
},
|
||||
};
|
||||
},
|
||||
forgotPassword: async (params) => {
|
||||
const response = await fetch(API_URL + "/users/forgot-password", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(params),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
if (response.status == 202) {
|
||||
return {
|
||||
success: true,
|
||||
redirectTo: "/",
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
},
|
||||
updatePassword: async (params) => {
|
||||
if (params.token !== undefined) {
|
||||
const response = await fetch(API_URL + "/users/reset-password", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
password: params.password,
|
||||
token: params.token,
|
||||
}),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
if (response.status == 200) {
|
||||
return {
|
||||
success: true,
|
||||
redirectTo: "/",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
},
|
||||
getPermissions: async () => { throw new Error("Not implemented"); },
|
||||
onError: async (error) => {
|
||||
if (error?.status === 401) {
|
||||
forget_user()
|
||||
return Promise<{
|
||||
redirectTo: "/login",
|
||||
logout: true,
|
||||
error: { message: "Authentication required" },
|
||||
}>;
|
||||
}
|
||||
else if (error?.status === 403) {
|
||||
return Promise<{
|
||||
error: { message: "Insufficient credentials" },
|
||||
}>;
|
||||
}
|
||||
return {};
|
||||
},
|
||||
};
|
||||
|
||||
function store_user(user: any) {
|
||||
localStorage.setItem(LOCAL_STORAGE_USER_KEY, JSON.stringify(user));
|
||||
}
|
||||
|
||||
function get_user() {
|
||||
const user_string = localStorage.getItem(LOCAL_STORAGE_USER_KEY)
|
||||
if (user_string == null) {
|
||||
return null
|
||||
}
|
||||
return JSON.parse(user_string);
|
||||
}
|
||||
|
||||
function forget_user() {
|
||||
localStorage.removeItem(LOCAL_STORAGE_USER_KEY);
|
||||
}
|
||||
|
||||
function findGetParameter(parameterName: string) {
|
||||
let result = null, tmp = [];
|
||||
location.search.substr(1).split("&")
|
||||
.forEach(function (item) {
|
||||
tmp = item.split("=");
|
||||
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user