mirror of
https://github.com/Dan6erbond/sk-auth.git
synced 2025-08-03 13:48:09 +02:00
🎨 Enable TS strict
mode and set target to es2017
This commit is contained in:
parent
7a9a9c48c9
commit
bd4ae4387e
@ -12,6 +12,7 @@ module.exports = {
|
|||||||
"@typescript-eslint/explicit-module-boundary-types": "off",
|
"@typescript-eslint/explicit-module-boundary-types": "off",
|
||||||
"@typescript-eslint/no-explicit-any": "off",
|
"@typescript-eslint/no-explicit-any": "off",
|
||||||
"@typescript-eslint/no-unused-vars": ["off", { argsIgnorePattern: "^_" }],
|
"@typescript-eslint/no-unused-vars": ["off", { argsIgnorePattern: "^_" }],
|
||||||
|
"@typescript-eslint/no-non-null-assertion": "off",
|
||||||
},
|
},
|
||||||
env: {
|
env: {
|
||||||
browser: true,
|
browser: true,
|
||||||
|
12
src/auth.ts
12
src/auth.ts
@ -115,8 +115,7 @@ export class Auth {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const jwt = this.signToken(token);
|
const jwt = this.signToken(token);
|
||||||
console.log(jwt);
|
const redirect = await this.getRedirectUrl(host, redirectUrl ?? undefined);
|
||||||
const redirect = await this.getRedirectUrl(host, redirectUrl);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
status: 302,
|
status: 302,
|
||||||
@ -158,9 +157,9 @@ export class Auth {
|
|||||||
|
|
||||||
const match = path.match(/\/api\/auth\/(?<method>signin|callback)\/(?<provider>\w+)/);
|
const match = path.match(/\/api\/auth\/(?<method>signin|callback)\/(?<provider>\w+)/);
|
||||||
|
|
||||||
if (match) {
|
if (match && match.groups) {
|
||||||
const provider = this.config?.providers?.find(
|
const provider = this.config?.providers?.find(
|
||||||
(provider) => provider.id === match.groups.provider,
|
(provider) => provider.id === match.groups!.provider,
|
||||||
);
|
);
|
||||||
if (provider) {
|
if (provider) {
|
||||||
if (match.groups.method === "signin") {
|
if (match.groups.method === "signin") {
|
||||||
@ -170,6 +169,11 @@ export class Auth {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: 404,
|
||||||
|
body: "Not found.",
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
get: RequestHandler = async (request) => {
|
get: RequestHandler = async (request) => {
|
||||||
|
@ -19,17 +19,19 @@ export async function signIn(provider: string, data?: any, config?: SignInConfig
|
|||||||
return await res.json();
|
return await res.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
let redirectUrl: string;
|
let redirectUrl: string | undefined;
|
||||||
if (config?.redirectUrl) {
|
if (config?.redirectUrl) {
|
||||||
redirectUrl = config.redirectUrl;
|
redirectUrl = config.redirectUrl;
|
||||||
} else {
|
} else {
|
||||||
let $val: Page;
|
let $val: Page | undefined;
|
||||||
page.subscribe(($) => ($val = $))();
|
page.subscribe(($) => ($val = $))();
|
||||||
redirectUrl = `${$val.host}${$val.path}?${$val.query}`;
|
if ($val) {
|
||||||
|
redirectUrl = `${$val.host}${$val.path}?${$val.query}`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const queryData = {
|
const queryData = {
|
||||||
redirect: redirectUrl,
|
redirect: redirectUrl ?? "/",
|
||||||
};
|
};
|
||||||
const query = new URLSearchParams(queryData);
|
const query = new URLSearchParams(queryData);
|
||||||
const path = `/api/auth/login/${provider}?${query}`;
|
const path = `/api/auth/login/${provider}?${query}`;
|
||||||
|
@ -11,7 +11,7 @@ export abstract class Provider<T extends ProviderConfig = ProviderConfig> {
|
|||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
constructor(protected readonly config: T) {
|
constructor(protected readonly config: T) {
|
||||||
this.id = config.id;
|
this.id = config.id!;
|
||||||
}
|
}
|
||||||
|
|
||||||
getUri(host: string, path: string) {
|
getUri(host: string, path: string) {
|
||||||
|
@ -28,7 +28,7 @@ export class FacebookAuthProvider extends OAuth2Provider<FacebookAuthProviderCon
|
|||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
client_id: this.config.clientId,
|
client_id: this.config.clientId,
|
||||||
scope: this.config.scope,
|
scope: this.config.scope!,
|
||||||
redirect_uri: this.getCallbackUri(host),
|
redirect_uri: this.getCallbackUri(host),
|
||||||
state,
|
state,
|
||||||
};
|
};
|
||||||
@ -69,7 +69,7 @@ export class FacebookAuthProvider extends OAuth2Provider<FacebookAuthProviderCon
|
|||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
access_token: tokens.access_token,
|
access_token: tokens.access_token,
|
||||||
fields: this.config.userProfileFields,
|
fields: this.config.userProfileFields!,
|
||||||
};
|
};
|
||||||
|
|
||||||
const res = await fetch(`${endpoint}?${new URLSearchParams(data)}`);
|
const res = await fetch(`${endpoint}?${new URLSearchParams(data)}`);
|
||||||
|
@ -23,7 +23,7 @@ export class GoogleOAuthProvider extends OAuth2Provider<GoogleOAuthProviderConfi
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getProviderMetadata() {
|
async getProviderMetadata() {
|
||||||
const res = await fetch(this.config.discoveryDocument);
|
const res = await fetch(this.config.discoveryDocument!);
|
||||||
const metadata = await res.json();
|
const metadata = await res.json();
|
||||||
return metadata;
|
return metadata;
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ export class GoogleOAuthProvider extends OAuth2Provider<GoogleOAuthProviderConfi
|
|||||||
const data = {
|
const data = {
|
||||||
response_type: "code",
|
response_type: "code",
|
||||||
client_id: this.config.clientId,
|
client_id: this.config.clientId,
|
||||||
scope: this.config.scope,
|
scope: this.config.scope!,
|
||||||
redirect_uri: this.getCallbackUri(host),
|
redirect_uri: this.getCallbackUri(host),
|
||||||
state,
|
state,
|
||||||
login_hint: "example@provider.com",
|
login_hint: "example@provider.com",
|
||||||
|
@ -35,7 +35,7 @@ export abstract class OAuth2Provider<T extends OAuth2ProviderConfig> extends Pro
|
|||||||
|
|
||||||
getStateValue(query: URLSearchParams, name: string) {
|
getStateValue(query: URLSearchParams, name: string) {
|
||||||
if (query.get("state")) {
|
if (query.get("state")) {
|
||||||
const state = Buffer.from(query.get("state"), "base64").toString();
|
const state = Buffer.from(query.get("state")!, "base64").toString();
|
||||||
return state
|
return state
|
||||||
.split(",")
|
.split(",")
|
||||||
.find((state) => state.startsWith(`${name}=`))
|
.find((state) => state.startsWith(`${name}=`))
|
||||||
@ -47,7 +47,7 @@ export abstract class OAuth2Provider<T extends OAuth2ProviderConfig> extends Pro
|
|||||||
const code = query.get("code");
|
const code = query.get("code");
|
||||||
const redirect = this.getStateValue(query, "redirect");
|
const redirect = this.getStateValue(query, "redirect");
|
||||||
|
|
||||||
const tokens = await this.getTokens(code, this.getCallbackUri(host));
|
const tokens = await this.getTokens(code!, this.getCallbackUri(host));
|
||||||
let user = await this.getUserProfile(tokens);
|
let user = await this.getUserProfile(tokens);
|
||||||
|
|
||||||
if (this.config.profile) {
|
if (this.config.profile) {
|
||||||
|
@ -79,8 +79,8 @@ export class RedditOAuthProvider extends OAuth2Provider<RedditOAuthProviderConfi
|
|||||||
response_type: "code",
|
response_type: "code",
|
||||||
state,
|
state,
|
||||||
redirect_uri: this.getCallbackUri(host),
|
redirect_uri: this.getCallbackUri(host),
|
||||||
duration: this.config.duration,
|
duration: this.config.duration!,
|
||||||
scope: this.config.scope,
|
scope: this.config.scope!,
|
||||||
};
|
};
|
||||||
|
|
||||||
const url = `${endpoint}?${new URLSearchParams(data)}`;
|
const url = `${endpoint}?${new URLSearchParams(data)}`;
|
||||||
|
@ -75,7 +75,7 @@ export class TwitterAuthProvider extends OAuth2Provider<TwitterAuthProviderConfi
|
|||||||
const oauthVerifier = query.get("oauth_verifier");
|
const oauthVerifier = query.get("oauth_verifier");
|
||||||
const redirect = this.getStateValue(query, "redirect");
|
const redirect = this.getStateValue(query, "redirect");
|
||||||
|
|
||||||
const tokens = await this.getTokens(oauthToken, oauthVerifier);
|
const tokens = await this.getTokens(oauthToken!, oauthVerifier!);
|
||||||
let user = await this.getUserProfile(tokens);
|
let user = await this.getUserProfile(tokens);
|
||||||
|
|
||||||
if (this.config.profile) {
|
if (this.config.profile) {
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "es5",
|
"target": "es2017",
|
||||||
"module": "es6",
|
"module": "es6",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
|
"strict": true,
|
||||||
|
"noImplicitAny": false,
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"removeComments": true,
|
"removeComments": true,
|
||||||
"emitDecoratorMetadata": true,
|
"emitDecoratorMetadata": true,
|
||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
"allowSyntheticDefaultImports": true,
|
"allowSyntheticDefaultImports": true,
|
||||||
"target": "es2017",
|
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
"baseUrl": "./",
|
"baseUrl": "./",
|
||||||
|
Loading…
Reference in New Issue
Block a user