feat: update and improve auth composable
This commit is contained in:
parent
c9decc585e
commit
2f0ff0521f
1 changed files with 26 additions and 21 deletions
|
@ -1,12 +1,17 @@
|
||||||
|
import type { UserResponse } from "~/types/interfaces";
|
||||||
|
|
||||||
export const useAuth = () => {
|
export const useAuth = () => {
|
||||||
const config = useRuntimeConfig();
|
|
||||||
const accessToken = useCookie("access_token");
|
const accessToken = useCookie("access_token");
|
||||||
const user = useState("user", () => null);
|
const user = useState<UserResponse | null>("user", () => null);
|
||||||
|
|
||||||
|
async function clearAuth() {
|
||||||
|
accessToken.value = null;
|
||||||
|
user.value = null;
|
||||||
|
}
|
||||||
|
|
||||||
async function register(username: string, email: string, password: string) {
|
async function register(username: string, email: string, password: string) {
|
||||||
const apiBase = localStorage.getItem("apiBase");
|
|
||||||
const hashedPass = await hashPassword(password);
|
const hashedPass = await hashPassword(password);
|
||||||
const res = await $fetch(`${apiBase}/auth/register`, {
|
const res = await fetchWithApi("/auth/register", {
|
||||||
method: "POST", body:
|
method: "POST", body:
|
||||||
{
|
{
|
||||||
email, identifier: username, password: hashedPass, device_name: "Linux Laptop"
|
email, identifier: username, password: hashedPass, device_name: "Linux Laptop"
|
||||||
|
@ -17,30 +22,28 @@ export const useAuth = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function login(username: string, password: string, device_name: string) {
|
async function login(username: string, password: string, device_name: string) {
|
||||||
const apiBase = localStorage.getItem("apiBase");
|
|
||||||
const hashedPass = await hashPassword(password);
|
const hashedPass = await hashPassword(password);
|
||||||
console.log("hashedPass:", hashedPass);
|
console.log("hashedPass:", hashedPass);
|
||||||
//authStore.setAccessToken(accessToken);
|
//authStore.setAccessToken(accessToken);
|
||||||
const res = await fetchWithAuth(`${apiBase}/auth/login`, {
|
const res = await fetchWithApi("/auth/login", {
|
||||||
method: "POST", body:
|
method: "POST", body:
|
||||||
{
|
{
|
||||||
username, password: hashedPass, device_name: "Linux Laptop"
|
username, password: hashedPass, device_name: "Linux Laptop"
|
||||||
}
|
}
|
||||||
}) as { access_token: string, refresh_token: string }; fetch
|
}) as { access_token: string, refresh_token: string }; fetch
|
||||||
|
console.log("hi");
|
||||||
accessToken.value = res.access_token;
|
accessToken.value = res.access_token;
|
||||||
console.log("access token:", accessToken.value);
|
console.log("access token:", accessToken.value);
|
||||||
await fetchUser();
|
await fetchUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function logout(password: string) {
|
async function logout(password: string) {
|
||||||
const apiBase = localStorage.getItem("apiBase");
|
|
||||||
console.log("password:", password);
|
console.log("password:", password);
|
||||||
console.log("access:", accessToken.value);
|
console.log("access:", accessToken.value);
|
||||||
const hashedPass = await hashPassword(password);
|
const hashedPass = await hashPassword(password);
|
||||||
console.log("hashed");
|
console.log("hashed");
|
||||||
|
|
||||||
const res = await fetchWithAuth(`${apiBase}/auth/revoke`, {
|
const res = await fetchWithApi("/auth/revoke", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body:
|
body:
|
||||||
{
|
{
|
||||||
|
@ -48,29 +51,31 @@ export const useAuth = () => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
accessToken.value = null;
|
clearAuth();
|
||||||
user.value = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function revoke() {
|
async function revoke() {
|
||||||
accessToken.value = null;
|
clearAuth();
|
||||||
localStorage.removeItem("instanceUrl");
|
|
||||||
localStorage.removeItem("apiBase");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function refresh() {
|
async function refresh() {
|
||||||
const apiBase = localStorage.getItem("apiBase");
|
console.log("refreshing");
|
||||||
const res = await fetchWithAuth(`${apiBase}/auth/refresh`, {
|
try {
|
||||||
method: "POST"
|
const res = await fetchWithApi("/auth/refresh", {
|
||||||
}) as { access_token: string };
|
method: "POST"
|
||||||
accessToken.value = res.access_token;
|
}) as { access_token: string };
|
||||||
|
accessToken.value = res.access_token;
|
||||||
|
console.log("set new access token");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("refresh error:", error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchUser() {
|
async function fetchUser() {
|
||||||
const apiBase = localStorage.getItem("apiBase");
|
|
||||||
if (!accessToken.value) return;
|
if (!accessToken.value) return;
|
||||||
const res = await fetchWithAuth(`${apiBase}/users/me`) as any;
|
const res = await fetchWithApi("/users/me") as UserResponse;
|
||||||
user.value = res;
|
user.value = res;
|
||||||
|
return user.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getUser() {
|
async function getUser() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue