Skip to content

Commit 9a5dbe2

Browse files
lulznekoDan6erbond
andauthored
✨ Upgrade to SvelteKit 1.0.0-next.211 and related fixes (#58)
* ✨ Upgrade to SvelteKit 1.0.0-next.211 and related fixes * 🚨 Fix some lint / formatter errors, ESLint plugin Svelte3 still not working Co-authored-by: RaviAnand Mohabir <moravrav@gmail.com>
1 parent 7c3d02e commit 9a5dbe2

File tree

11 files changed

+246
-126
lines changed

11 files changed

+246
-126
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ SvelteKitAuth also comes with first-class support for Typescript out of the box,
3131

3232
SvelteKitAuth is very easy to setup! All you need to do is instantiate the `SvelteKitAuth` class, and configure it with some default providers, as well as a JWT secret key used to verify the cookies:
3333

34-
_**Warning**: env variables prefixed with `VITE_` can be exposed and leaked into client-side bundles if they are referenced in any client-side code. Make sure this is not the case, or consider using an alternative method such as loading them via dotenv directly instead._
34+
_**Warning**: env variables prefixed with `VITE_` can be exposed and leaked into client-side bundles if they are referenced in any client-side code. Make sure this is not the case, or consider using an alternative method such as loading them via dotenv directly instead.\_
3535

3636
```ts
3737
export const appAuth = new SvelteKitAuth({

app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"dev": "svelte-kit dev",
66
"build": "svelte-kit build",
77
"preview": "svelte-kit preview",
8-
"lint": "prettier --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
8+
"lint": "prettier --check --plugin-search-dir=. . && eslint --ignore-path .gitignore --config .eslintrc.cjs .",
99
"format": "prettier --write --plugin-search-dir=. ."
1010
},
1111
"devDependencies": {

app/yarn.lock

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3441,12 +3441,6 @@ simple-swizzle@^0.2.2:
34413441
cookie "^0.4.1"
34423442
jsonwebtoken "^8.5.1"
34433443

3444-
"sk-auth@file:../":
3445-
version "0.3.7"
3446-
dependencies:
3447-
cookie "^0.4.1"
3448-
jsonwebtoken "^8.5.1"
3449-
34503444
slash@^3.0.0:
34513445
version "3.0.0"
34523446
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
},
4848
"devDependencies": {
4949
"@rollup/plugin-typescript": "^8.2.1",
50-
"@sveltejs/kit": "^1.0.0-next.107",
50+
"@sveltejs/kit": "^1.0.0-next.211",
5151
"@types/jsonwebtoken": "^8.5.1",
5252
"@typescript-eslint/eslint-plugin": "^4.23.0",
5353
"@typescript-eslint/parser": "^4.23.0",

src/auth.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { GetSession, RequestHandler } from "@sveltejs/kit";
2-
import type { EndpointOutput, ServerRequest } from "@sveltejs/kit/types/endpoint";
3-
import type { Headers } from "@sveltejs/kit/types/helper";
2+
import type { EndpointOutput } from "@sveltejs/kit/types/endpoint";
3+
import { RequestHeaders } from "@sveltejs/kit/types/helper";
4+
import { ServerRequest } from "@sveltejs/kit/types/hooks";
45
import cookie from "cookie";
56
import * as jsonwebtoken from "jsonwebtoken";
67
import type { JWT, Session } from "./interfaces";
@@ -43,7 +44,7 @@ export class Auth {
4344
return "svelte_auth_secret";
4445
}
4546

46-
async getToken(headers: Headers) {
47+
async getToken(headers: RequestHeaders) {
4748
if (!headers.cookie) {
4849
return null;
4950
}
@@ -82,7 +83,7 @@ export class Auth {
8283
return new URL(pathname, this.getBaseUrl(host)).href;
8384
}
8485

85-
setToken(headers: Headers, newToken: JWT | any) {
86+
setToken(headers: RequestHeaders, newToken: JWT | any) {
8687
const originalToken = this.getToken(headers);
8788

8889
return {
@@ -113,7 +114,7 @@ export class Auth {
113114
request: ServerRequest,
114115
provider: Provider,
115116
): Promise<EndpointOutput> {
116-
const { headers, host } = request;
117+
const { headers, url } = request;
117118
const [profile, redirectUrl] = await provider.callback(request, this);
118119

119120
let token = (await this.getToken(headers)) ?? { user: {} };
@@ -124,7 +125,7 @@ export class Auth {
124125
}
125126

126127
const jwt = this.signToken(token);
127-
const redirect = await this.getRedirectUrl(host, redirectUrl ?? undefined);
128+
const redirect = await this.getRedirectUrl(url.host, redirectUrl ?? undefined);
128129

129130
return {
130131
status: 302,
@@ -136,9 +137,9 @@ export class Auth {
136137
}
137138

138139
async handleEndpoint(request: ServerRequest): Promise<EndpointOutput> {
139-
const { path, headers, method, host } = request;
140+
const { headers, method, url } = request;
140141

141-
if (path === this.getPath("signout")) {
142+
if (url.pathname === this.getPath("signout")) {
142143
const token = this.setToken(headers, {});
143144
const jwt = this.signToken(token);
144145

@@ -153,7 +154,7 @@ export class Auth {
153154
};
154155
}
155156

156-
const redirect = await this.getRedirectUrl(host);
157+
const redirect = await this.getRedirectUrl(url.host);
157158

158159
return {
159160
status: 302,
@@ -165,7 +166,7 @@ export class Auth {
165166
}
166167

167168
const regex = new RegExp(join([this.basePath, `(?<method>signin|callback)/(?<provider>\\w+)`]));
168-
const match = path.match(regex);
169+
const match = url.pathname.match(regex);
169170

170171
if (match && match.groups) {
171172
const provider = this.config?.providers?.find(
@@ -187,11 +188,11 @@ export class Auth {
187188
}
188189

189190
get: RequestHandler = async (request) => {
190-
const { path } = request;
191+
const { url } = request;
191192

192-
if (path === this.getPath("csrf")) {
193+
if (url.pathname === this.getPath("csrf")) {
193194
return { body: "1234" }; // TODO: Generate real token
194-
} else if (path === this.getPath("session")) {
195+
} else if (url.pathname === this.getPath("session")) {
195196
const session = await this.getSession(request);
196197
return {
197198
body: {

src/client/signIn.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* import { goto } from "@sveltejs/kit/assets/runtime/app/navigation";
22
import { page } from "@sveltejs/kit/assets/runtime/app/stores"; */
3-
import type { Page } from "@sveltejs/kit";
3+
import type { LoadInput } from "@sveltejs/kit";
44

55
interface SignInConfig {
66
redirectUrl?: string;
@@ -23,10 +23,10 @@ export async function signIn(provider: string, data?: any, config?: SignInConfig
2323
if (config?.redirectUrl) {
2424
redirectUrl = config.redirectUrl;
2525
} else {
26-
let $val: Page | undefined;
26+
let $val: LoadInput | undefined;
2727
/* page.subscribe(($) => ($val = $))(); */
2828
if ($val) {
29-
redirectUrl = `${$val.host}${$val.path}?${$val.query}`;
29+
redirectUrl = `${$val.url.host}${$val.url.pathname}?${$val.url.searchParams}`;
3030
}
3131
}
3232

src/providers/base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { EndpointOutput } from "@sveltejs/kit";
2-
import type { ServerRequest } from "@sveltejs/kit/types/endpoint";
2+
import { ServerRequest } from "@sveltejs/kit/types/hooks";
33
import type { Auth } from "../auth";
44
import type { CallbackResult } from "../types";
55

src/providers/oauth2.base.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { EndpointOutput, ServerRequest } from "@sveltejs/kit/types/endpoint";
1+
import type { EndpointOutput } from "@sveltejs/kit/types/endpoint";
2+
import { ServerRequest } from "@sveltejs/kit/types/hooks";
23
import type { Auth } from "../auth";
34
import type { CallbackResult } from "../types";
45
import { Provider, ProviderConfig } from "./base";
@@ -33,24 +34,26 @@ export abstract class OAuth2BaseProvider<
3334
abstract getUserProfile(tokens: any): ProfileType | Promise<ProfileType>;
3435

3536
async signin(request: ServerRequest, auth: Auth): Promise<EndpointOutput> {
36-
const { method, host, query } = request;
37-
const state = [`redirect=${query.get("redirect") ?? this.getUri(auth, "/", host)}`].join(",");
37+
const { method, url } = request;
38+
const state = [
39+
`redirect=${url.searchParams.get("redirect") ?? this.getUri(auth, "/", url.host)}`,
40+
].join(",");
3841
const base64State = Buffer.from(state).toString("base64");
3942
const nonce = Math.round(Math.random() * 1000).toString(); // TODO: Generate random based on user values
40-
const url = await this.getAuthorizationUrl(request, auth, base64State, nonce);
43+
const authUrl = await this.getAuthorizationUrl(request, auth, base64State, nonce);
4144

4245
if (method === "POST") {
4346
return {
4447
body: {
45-
redirect: url,
48+
redirect: authUrl,
4649
},
4750
};
4851
}
4952

5053
return {
5154
status: 302,
5255
headers: {
53-
Location: url,
56+
Location: authUrl,
5457
},
5558
};
5659
}
@@ -65,17 +68,17 @@ export abstract class OAuth2BaseProvider<
6568
}
6669
}
6770

68-
async callback({ query, host }: ServerRequest, auth: Auth): Promise<CallbackResult> {
69-
const code = query.get("code");
70-
const redirect = this.getStateValue(query, "redirect");
71+
async callback({ url }: ServerRequest, auth: Auth): Promise<CallbackResult> {
72+
const code = url.searchParams.get("code");
73+
const redirect = this.getStateValue(url.searchParams, "redirect");
7174

72-
const tokens = await this.getTokens(code!, this.getCallbackUri(auth, host));
75+
const tokens = await this.getTokens(code!, this.getCallbackUri(auth, url.host));
7376
let user = await this.getUserProfile(tokens);
7477

7578
if (this.config.profile) {
7679
user = await this.config.profile(user, tokens);
7780
}
7881

79-
return [user, redirect ?? this.getUri(auth, "/", host)];
82+
return [user, redirect ?? this.getUri(auth, "/", url.host)];
8083
}
8184
}

src/providers/oauth2.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ServerRequest } from "@sveltejs/kit/types/endpoint";
1+
import { ServerRequest } from "@sveltejs/kit/types/hooks";
22
import type { Auth } from "../auth";
33
import { ucFirst } from "../helpers";
44
import { OAuth2BaseProvider, OAuth2BaseProviderConfig, OAuth2Tokens } from "./oauth2.base";
@@ -37,19 +37,19 @@ export class OAuth2Provider<
3737
});
3838
}
3939

40-
getAuthorizationUrl({ host }: ServerRequest, auth: Auth, state: string, nonce: string) {
40+
getAuthorizationUrl({ url }: ServerRequest, auth: Auth, state: string, nonce: string) {
4141
const data = {
4242
state,
4343
nonce,
4444
response_type: this.config.responseType,
4545
client_id: this.config.clientId,
4646
scope: Array.isArray(this.config.scope) ? this.config.scope.join(" ") : this.config.scope!,
47-
redirect_uri: this.getCallbackUri(auth, host),
47+
redirect_uri: this.getCallbackUri(auth, url.host),
4848
...(this.config.authorizationParams ?? {}),
4949
};
5050

51-
const url = `${this.config.authorizationUrl}?${new URLSearchParams(data)}`;
52-
return url;
51+
const authUrl = `${this.config.authorizationUrl}?${new URLSearchParams(data)}`;
52+
return authUrl;
5353
}
5454

5555
async getTokens(code: string, redirectUri: string): Promise<TokensType> {

src/providers/twitter.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ServerRequest } from "@sveltejs/kit/types/endpoint";
1+
import { ServerRequest } from "@sveltejs/kit/types/hooks";
22
import type { Auth } from "../auth";
33
import type { CallbackResult } from "../types";
44
import { OAuth2BaseProvider, OAuth2BaseProviderConfig } from "./oauth2.base";
@@ -38,17 +38,17 @@ export class TwitterAuthProvider extends OAuth2BaseProvider<any, any, TwitterAut
3838
};
3939
}
4040

41-
async getAuthorizationUrl({ host }: ServerRequest, auth: Auth, state: string, nonce: string) {
41+
async getAuthorizationUrl({ url }: ServerRequest, auth: Auth, state: string, nonce: string) {
4242
const endpoint = "https://api.twitter.com/oauth/authorize";
4343

44-
const { oauthToken } = await this.getRequestToken(auth, host);
44+
const { oauthToken } = await this.getRequestToken(auth, url.host);
4545

4646
const data = {
4747
oauth_token: oauthToken,
4848
};
4949

50-
const url = `${endpoint}?${new URLSearchParams(data)}`;
51-
return url;
50+
const authUrl = `${endpoint}?${new URLSearchParams(data)}`;
51+
return authUrl;
5252
}
5353

5454
async getTokens(oauthToken: string, oauthVerifier: string) {
@@ -71,10 +71,10 @@ export class TwitterAuthProvider extends OAuth2BaseProvider<any, any, TwitterAut
7171
return await res.json();
7272
}
7373

74-
async callback({ query, host }: ServerRequest, auth: Auth): Promise<CallbackResult> {
75-
const oauthToken = query.get("oauth_token");
76-
const oauthVerifier = query.get("oauth_verifier");
77-
const redirect = this.getStateValue(query, "redirect");
74+
async callback({ url }: ServerRequest, auth: Auth): Promise<CallbackResult> {
75+
const oauthToken = url.searchParams.get("oauth_token");
76+
const oauthVerifier = url.searchParams.get("oauth_verifier");
77+
const redirect = this.getStateValue(url.searchParams, "redirect");
7878

7979
const tokens = await this.getTokens(oauthToken!, oauthVerifier!);
8080
let user = await this.getUserProfile(tokens);
@@ -83,6 +83,6 @@ export class TwitterAuthProvider extends OAuth2BaseProvider<any, any, TwitterAut
8383
user = await this.config.profile(user, tokens);
8484
}
8585

86-
return [user, redirect ?? this.getUri(auth, "/", host)];
86+
return [user, redirect ?? this.getUri(auth, "/", url.host)];
8787
}
8888
}

0 commit comments

Comments
 (0)