-
Notifications
You must be signed in to change notification settings - Fork 980
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add first version of Nhost Auth * Add nhost auth to the CLI * Remove console.log * Remove unnecessary type * Final adjustments * Bump nhost-js-sdk * Fix restoreAuthState * Bump up nhost-js-sdk version * Add AuthClientNhost that implements AuthClient * Add Nhost to auth/README.md * Add description for params * Add NhostProvider type * Remove empty line * Remove skipFetchCurrentUser Co-authored-by: Tobbe Lundberg <tobbe@tlundberg.com> Co-authored-by: David Price <thedavid@thedavidprice.com>
- Loading branch information
1 parent
93f7aa4
commit 601b33a
Showing
7 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,5 +61,6 @@ export type SupportedAuthClients = | |
| Firebase | ||
| Supabase | ||
| Ethereum | ||
| Nhost | ||
| Custom | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import type { Session, NhostClient, User } from 'nhost-js-sdk' | ||
|
||
export type Nhost = NhostClient | ||
export type NhostUser = User | ||
|
||
import { AuthClient } from './' | ||
|
||
type NhostProvider = 'google' | 'github' | 'facebook' | 'linkedin' | ||
export interface AuthClientNhost extends AuthClient { | ||
/** | ||
* Log In an existing user with email/password or via a OAuth provider | ||
* Log In via a OAuth provider also registers the account in case it doesn't exist | ||
* @param options.email The user's email address | ||
* @param options.password The user's password | ||
* @param options.provider One of NhostProvider | ||
*/ | ||
login(options: { | ||
email?: string | ||
password?: string | ||
provider?: NhostProvider | ||
}): Promise<{ | ||
session: Session | null | ||
user: NhostUser | null | ||
mfa?: { | ||
ticket: string | ||
} | ||
}> | ||
logout(): Promise<void> | ||
/** | ||
* Creates a new user account | ||
* @param options.email The user's email address | ||
* @param options.password The user's password | ||
*/ | ||
signup(options: { | ||
email: string | ||
password: string | ||
registrationOptions?: { | ||
userData?: any | ||
defaultRole?: string | ||
allowedRoles?: string[] | ||
} | ||
}): Promise<{ | ||
session: Session | null | ||
user: User | null | ||
}> | ||
|
||
getToken(): Promise<string | null> | ||
getUserMetadata(): Promise<NhostUser | null> | ||
restoreAuthState(): Promise<{ | ||
session: Session | null | ||
user: NhostUser | null | ||
}> | ||
client: Nhost | ||
} | ||
|
||
export const nhost = (client: Nhost): AuthClient => { | ||
return { | ||
type: 'nhost', | ||
client, | ||
login: async ({ email, password, provider }) => { | ||
if (email && password) { | ||
return await client.auth.login({ email, password }) | ||
} | ||
|
||
if (provider) { | ||
return await client.auth.login({ provider }) | ||
} | ||
|
||
throw new Error( | ||
'You must provide an email/password or a third-party OAuth provider.' | ||
) | ||
}, | ||
logout: async () => { | ||
return await client.auth.logout() | ||
}, | ||
signup: async ({ email, password }) => { | ||
return await client.auth.register({ | ||
email, | ||
password, | ||
registrationOptions: { userData: { display_name: email } }, | ||
}) | ||
}, | ||
getToken: async () => { | ||
return await client.auth.getJWTToken() | ||
}, | ||
getUserMetadata: async () => { | ||
return await client.auth.user() | ||
}, | ||
restoreAuthState: async () => { | ||
return await client.auth.refreshSession() | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// the lines that need to be added to index.js | ||
export const config = { | ||
imports: [`import { createClient } from 'nhost-js-sdk'`], | ||
init: `const nhostClient = createClient({ | ||
baseURL: process.env.NHOST_BACKEND_URL, | ||
autoLogin: false, | ||
}); | ||
`, | ||
authProvider: { | ||
client: 'nhostClient', | ||
type: 'nhost', | ||
}, | ||
} | ||
|
||
// required packages to install | ||
export const webPackages = ['nhost-js-sdk'] | ||
export const apiPackages = [] | ||
|
||
// any notes to print out when the job is done | ||
export const notes = [ | ||
"You will need to add your project's backend URL (NHOST_BACKEND_URL) to your .env file.", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters