Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add webauth & magic links to sveltekit #973

Merged
merged 5 commits into from May 17, 2024

Conversation

diksipav
Copy link
Contributor

@diksipav diksipav commented Apr 18, 2024

I tested this with example sveltekit and it works.

I retested and it works, but not sure is the setCookie function definition the best, especially the params it needs, doesn't look very pretty.

@diksipav diksipav requested a review from jaclarke April 18, 2024 16:43
Copy link
Collaborator

@scotttrinh scotttrinh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! A couple of small things once you rebase this on the latest master.

packages/auth-sveltekit/src/server.ts Outdated Show resolved Hide resolved
packages/auth-sveltekit/src/server.ts Show resolved Hide resolved
packages/auth-sveltekit/src/server.ts Outdated Show resolved Hide resolved
@diksipav diksipav force-pushed the add-webauth-to-auth-sveltekit branch from 24a91bf to 06d1773 Compare May 8, 2024 16:36
@diksipav diksipav requested a review from scotttrinh May 8, 2024 16:49
packages/auth-sveltekit/src/server.ts Outdated Show resolved Hide resolved
packages/auth-sveltekit/src/server.ts Outdated Show resolved Hide resolved
@diksipav diksipav requested a review from scotttrinh May 8, 2024 19:00
cookies: Cookies,
value: string,
config: AuthConfig,
isAuth = false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I prefer two separate functions here than a boolean with a default value. If you want to share the base configuration object between both functions, that's fine! Something like:

const BASE_COOKIE_CONFIG = {
  httpOnly: true,
  sameSite: "lax",
  path: "/",
};

then in the functions:

function setVerifierCookie(cookies: Cookies, config: AuthConfig, value: string): void {
  const expires = new Date(Date.now() + 1000 * 60 * 24 * 7); // in 7 days
  cookies.set(config.pkceVerifierCookieName, value, {
    ...BASE_COOKIE_CONFIG,
    expires,
    secure: config.baseUrl.startsWith("https"),
  });
}

Then in the class you can:

private setVerifierCookie(verifier: string) {
  setVerifierCookie(this.cookies, this.config, verifier);
}

The advantage here is that you can look at the call site and know which cookie it's setting without knowing if the optional true means verifier or auth token, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I agree with all of these, will update : )

Use separate functions for verifier and auth token.
@diksipav diksipav requested a review from scotttrinh May 17, 2024 14:19
Comment on lines -135 to -155
private setVerifierCookie(verifier: string) {
const expires = new Date(Date.now() + 1000 * 60 * 24 * 7); // In 7 days
this.cookies.set(this.config.pkceVerifierCookieName, verifier, {
httpOnly: true,
sameSite: "lax",
path: "/",
expires,
secure: this.isSecure,
});
}

private setAuthCookie(authToken: string) {
const expires = Auth.getTokenExpiration(authToken);
this.cookies.set(this.config.authCookieName, authToken, {
httpOnly: true,
sameSite: "lax",
path: "/",
expires: expires ?? undefined,
secure: this.isSecure,
});
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker, but the change here would be smaller and easier to follow for future endpoints if we kept these class methods and just had them call the new function, so that other methods could continue to just call this.setAuthCookie(tokenData.auth_token), etc.

  private setVerifierCookie(verifier: string) {
    setVerifierCookie(this.cookies, this.config, verifier);
  }

  private setAuthCookie(authToken: string) {
    setAuthCookie(this.cookies, this.config, authToken);
  }

And for bonus points doing a similar thing for the delete cookie functionality:

  private deleteVerifierCookie() {
    deleteCookie(this.cookies, this.config.pkceVerifierCookieName);
  }

  private deleteAuthCookie() {
    deleteCookie(this.cookies, this.config.authCookieName);
  }

Copy link
Contributor Author

@diksipav diksipav May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm yes, this is even more cleaner (for cookies inside class methods), will update : )
but still we have to use functions for cookies outside the class

@diksipav diksipav requested a review from scotttrinh May 17, 2024 15:05
Copy link
Collaborator

@scotttrinh scotttrinh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏆

@diksipav diksipav merged commit 2e2f007 into master May 17, 2024
10 checks passed
@diksipav diksipav deleted the add-webauth-to-auth-sveltekit branch May 17, 2024 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants