-
Notifications
You must be signed in to change notification settings - Fork 975
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 @redwoodjs/auth package #497
Conversation
context: NetlifyClientContext | ||
}) => { | ||
const type = event?.headers[REDWOOD_AUTH_TYPE_HEADER] as SupportedAuthTypes | ||
switch (type) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If using an enum
above for SupportAuthTypes
, could use something like https://github.com/UselessPickles/ts-enum-util here
|
||
export interface AuthClient { | ||
restoreAuthState?(): void | Promise<any> | ||
login(options?: any): Promise<any> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add TODO to add options
typing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually had some trouble here and would love to hear how you would solve this:
Auth0 does not take arguments for the login function, but Netlify does. I thought I could solve this with function overloading, but I realise that this is probably not for functions associated to an object?
So I added this random "options" argument that could be anything, and added a better type definition in AuthClientNetlify
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you're saying. Even if you mark options
as optional with a type, that still implies the Auth0 version would accept the options
arg.
You could do something like this:
export interface AuthClient {
restoreAuthState?(): void | Promise<any>
login(): Promise<any>
logout(): void | Promise<void>
}
export type AuthClientAuth0 = AuthClient
export interface AuthClientNetlify extends Omit<AuthClient, 'login'> {
login(options: {
email: string
password: string
remember?: boolean
}): Promise<NetlifyUser>
client: Netlify
}
...only problem there is that Netlify Auth Clients will need to do something like
(client as AuthClientNetlify).login(args)
but it's possible a consumer would've already cast it anyway
client: SupportedAuthClients, | ||
type: SupportedAuthTypes | ||
): AuthClient => { | ||
switch (type) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment on enum versus types as above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @jmreidy; I had a look at this and I wanted these to be enums that are backed as string, because I the auth type as a header value, and I couldn't really find a good motivation for converting them to enums.
Is there something that I haven't really thought about?
"outDir": "dist", | ||
"rootDir": "src", | ||
// gotrue-js does not return types for `removeSavedSession` and `recoverSession` | ||
"noImplicitAny": false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Boo. Could we extend with a .d.ts
module? Alternatively I'd be happy to contribute by adding those types upstream to gotrue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, this is my first real TS project and I had some trouble figuring out what to do in this case - it seemed like contributing upstream was the recommended way, but hadn't considered fixing it via a .d.ts
file.
I'll give that a go!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hope I'm not jumping the gun on providing feedback. This is awesome work, and it's so exciting to see it in TS! Thanks so much for driving this @peterp ! 💪
@jmreidy this is totally helpful, thank you! |
Web side
The first step is to pick which authentication library you want to use: Netlify Identity, GoTrue, or Auth0 are currently supported.
As an example I'll pick Netlify Identity, and wrap the
RedwoodProvider
in ourAuthProvider
.Redwood's auth package offers a hook based API:
Protecting Routes: We're introducing a new
PrivateRoute
component that gate keeps unauthenticated users.API side
The
currentUser
is automatically added to the context of each request in the GraphQL server.@redwoodjs/auth
from example-invoice.#214