mirror of
https://github.com/Dan6erbond/sk-auth.git
synced 2024-11-20 19:07:20 +01:00
parent
099dfd294f
commit
ac77764b3d
19
src/client/helpers.ts
Normal file
19
src/client/helpers.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
function mergePath(basePaths: (string | null)[], path: string) {
|
||||||
|
if (path.startsWith("/")) {
|
||||||
|
path = path.slice(1);
|
||||||
|
}
|
||||||
|
let retPath;
|
||||||
|
for (let basePath of basePaths) {
|
||||||
|
if (basePath !== null) {
|
||||||
|
if (!basePath.startsWith("/")) {
|
||||||
|
basePath = "/" + basePath;
|
||||||
|
}
|
||||||
|
if (!basePath.endsWith("/")) {
|
||||||
|
basePath = basePath + "/";
|
||||||
|
}
|
||||||
|
retPath = basePath + path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return retPath;
|
||||||
|
}
|
@ -1,14 +1,15 @@
|
|||||||
/* import { goto } from "@sveltejs/kit/assets/runtime/app/navigation";
|
/* import { goto } from "@sveltejs/kit/assets/runtime/app/navigation";
|
||||||
import { page } from "@sveltejs/kit/assets/runtime/app/stores"; */
|
import { page } from "@sveltejs/kit/assets/runtime/app/stores"; */
|
||||||
import type { LoadInput } from "@sveltejs/kit";
|
import type { LoadInput } from "@sveltejs/kit";
|
||||||
|
import type { ClientRequestConfig } from "./types";
|
||||||
|
|
||||||
interface SignInConfig {
|
interface SignInConfig extends ClientRequestConfig {
|
||||||
redirectUrl?: string;
|
redirectUrl?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function signIn(provider: string, data?: any, config?: SignInConfig) {
|
export async function signIn(provider: string, data?: any, config?: SignInConfig) {
|
||||||
if (data) {
|
if (data) {
|
||||||
const path = `/api/auth/callback/${provider}`;
|
const path = mergePath(["/api/auth", config?.basePath ?? null], `/callback/${provider}`);
|
||||||
const res = await fetch(path, {
|
const res = await fetch(path, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
@ -34,7 +35,7 @@ export async function signIn(provider: string, data?: any, config?: SignInConfig
|
|||||||
redirect: redirectUrl ?? "/",
|
redirect: redirectUrl ?? "/",
|
||||||
};
|
};
|
||||||
const query = new URLSearchParams(queryData);
|
const query = new URLSearchParams(queryData);
|
||||||
const path = `/api/auth/login/${provider}?${query}`;
|
const path = mergePath(["/api/auth", config?.basePath ?? null], `/signin/${provider}?${query}`);
|
||||||
|
|
||||||
return path; // await goto(path);
|
return path; // await goto(path);
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,18 @@
|
|||||||
/* import { session as session$ } from "$app/stores"; */
|
/* import { session as session$ } from "$app/stores"; */
|
||||||
|
|
||||||
export async function signOut() {
|
import type { ClientRequestConfig } from "./types";
|
||||||
let res = await fetch("/api/auth/signout", { method: "POST" });
|
|
||||||
|
export async function signOut(config?: ClientRequestConfig) {
|
||||||
|
let res = await fetch(mergePath(["/api/auth", config?.basePath ?? null], "signout"), {
|
||||||
|
method: "POST",
|
||||||
|
});
|
||||||
const { signout } = await res.json();
|
const { signout } = await res.json();
|
||||||
|
|
||||||
if (!signout) {
|
if (!signout) {
|
||||||
throw new Error("Sign out not successful!");
|
throw new Error("Sign out not successful!");
|
||||||
}
|
}
|
||||||
|
|
||||||
res = await fetch("/api/auth/session");
|
res = await fetch(mergePath(["/api/auth", config?.basePath ?? null], "session"));
|
||||||
const session = await res.json();
|
const session = await res.json();
|
||||||
|
|
||||||
return session;
|
return session;
|
||||||
|
3
src/client/types.ts
Normal file
3
src/client/types.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export interface ClientRequestConfig {
|
||||||
|
basePath?: string;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user