🐛 Refactor getPath to getUrl and add getPath to fix detection of routes

This commit is contained in:
RaviAnand Mohabir 2021-05-24 16:20:12 +02:00
parent 35f48c0cb5
commit 98f73f36ea
2 changed files with 16 additions and 7 deletions

View File

@ -26,6 +26,10 @@ interface AuthCallbacks {
export class Auth {
constructor(private readonly config?: AuthConfig) {}
get basePath() {
return this.config?.basePath ?? "/api/auth";
}
getJwtSecret() {
if (this.config?.jwtSecret) {
return this.config?.jwtSecret;
@ -68,9 +72,14 @@ export class Auth {
return this.config?.host ?? `http://${host}`;
}
getPath(path: string, host?: string) {
const uri = join([this.config?.basePath ?? "/api/auth", path]);
return new URL(uri, this.getBaseUrl(host)).pathname;
getPath(path: string) {
const pathname = join([this.basePath, path]);
return pathname;
}
getUrl(path: string, host?: string) {
const pathname = this.getPath(path);
return new URL(pathname, this.getBaseUrl(host)).href;
}
setToken(headers: Headers, newToken: JWT | any) {
@ -129,7 +138,7 @@ export class Auth {
async handleEndpoint(request: ServerRequest): Promise<EndpointOutput> {
const { path, headers, method, host } = request;
if (path === this.getPath("signout", host)) {
if (path === this.getPath("signout")) {
const token = this.setToken(headers, {});
const jwt = this.signToken(token);

View File

@ -16,15 +16,15 @@ export abstract class Provider<T extends ProviderConfig = ProviderConfig> {
}
getUri(svelteKitAuth: Auth, path: string, host?: string) {
return `http://${host}${path}`;
return svelteKitAuth.getUrl(path, host);
}
getCallbackUri(svelteKitAuth: Auth, host?: string) {
return svelteKitAuth.getPath(`${"/api/auth/callback/"}${this.id}`, host);
return this.getUri(svelteKitAuth, `${"/callback/"}${this.id}`, host);
}
getSigninUri(svelteKitAuth: Auth, host?: string) {
return svelteKitAuth.getPath(`${"/api/auth/signin/"}${this.id}`, host);
return this.getUri(svelteKitAuth, `${"/signin/"}${this.id}`, host);
}
abstract signin<Locals extends Record<string, any> = Record<string, any>, Body = unknown>(