Skip to content

Commit 7d1dc62

Browse files
Add hooks.ts boilerplate
1 parent 73213a1 commit 7d1dc62

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/hooks.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {
2+
ConfigurePassportOAuth2,
3+
OAuthHandleInput,
4+
OAuthCreateCookie,
5+
DefaultCookieName
6+
} from 'sveltekit-passport-oauth2';
7+
import GitlabStrategy from "passport-gitlab2";
8+
9+
ConfigurePassportOAuth2([
10+
{
11+
strategy: new GitlabStrategy(
12+
{
13+
callbackURL: 'http://127.0.0.1:3000/auth/gitlab/callback.json',
14+
clientID: process.env.GITLAB_CLIENT_ID,
15+
clientSecret: process.env.GITLAB_CLIENT_SECRET,
16+
// passReqToCallback: true,
17+
baseURL: process.env.GITLAB_BASEURL
18+
},
19+
OAuthCreateCookie(/*cookieName = DefaultCookieName, cookieSettings?: CookieSettings*/)
20+
)
21+
}
22+
]);
23+
export const handle = sequence(addUserToRequest);
24+
25+
//add the user info to request (you can access this info in and endpoint using `request.locals`)
26+
async function addUserToRequest({ request, resolve }: OAuthHandleInput) {
27+
const cookies = cookie.parse(request.headers.cookie || '');
28+
const cookieId = cookies[DefaultCookieName];
29+
if (cookieId) {
30+
request.locals = Database[cookieId]; //change this to retrieve from database
31+
}
32+
const response = await resolve(request);
33+
return response;
34+
}
35+
36+
//this will expose user info in session and can be retrieved in the front end using $session
37+
export function getSession(request: ServerRequest<Locals>): Locals {
38+
return request.locals;
39+
}

0 commit comments

Comments
 (0)