Skip to content

Commit

Permalink
Nhost Auth 1.0 (#1725)
Browse files Browse the repository at this point in the history
* 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
3 people committed Feb 22, 2021
1 parent 93f7aa4 commit 601b33a
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/api/src/auth/decoders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const typesToDecoders: Record<
firebase: noop,
supabase: supabase,
ethereum: ethereum,
nhost: noop,
custom: noop,
}

Expand Down
1 change: 1 addition & 0 deletions packages/auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ export type SupportedAuthClients =
| Firebase
| Supabase
| Ethereum
| Nhost
| Custom
```
1 change: 1 addition & 0 deletions packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"magic-sdk": "^2.5.0",
"msal": "^1.4.1",
"netlify-identity-widget": "1.9.1",
"nhost-js-sdk": "^3.0.0-14",
"react": "^16.13.1"
},
"bundlesize": [
Expand Down
6 changes: 6 additions & 0 deletions packages/auth/src/authClients/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import type { MagicLink, MagicUser } from './magicLink'
import { magicLink } from './magicLink'
import type { NetlifyIdentity } from './netlify'
import { netlify } from './netlify'
import type { Nhost, NhostUser } from './nhost'
import { nhost } from './nhost'
import type { Supabase, SupabaseUser } from './supabase'
import { supabase } from './supabase'

Expand All @@ -29,6 +31,7 @@ const typesToClients = {
firebase,
supabase,
ethereum,
nhost,
/** Don't we support your auth client? No problem, define your own the `custom` type! */
custom,
}
Expand All @@ -42,6 +45,7 @@ export type SupportedAuthClients =
| Firebase
| Supabase
| Ethereum
| Nhost
| Custom

export type SupportedAuthTypes = keyof typeof typesToClients
Expand All @@ -52,13 +56,15 @@ export type { GoTrueUser }
export type { MagicUser }
export type { SupabaseUser }
export type { EthereumUser }
export type { NhostUser }
export type SupportedUserMetadata =
| Auth0User
| AzureActiveDirectoryUser
| GoTrueUser
| MagicUser
| SupabaseUser
| EthereumUser
| NhostUser

export interface AuthClient {
restoreAuthState?(): void | Promise<any>
Expand Down
93 changes: 93 additions & 0 deletions packages/auth/src/authClients/nhost.ts
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()
},
}
}
22 changes: 22 additions & 0 deletions packages/cli/src/commands/setup/auth/providers/nhost.js
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.",
]
41 changes: 41 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4130,6 +4130,11 @@
dependencies:
"@types/node" "*"

"@types/jwt-decode@^2.2.1":
version "2.2.1"
resolved "https://registry.yarnpkg.com/@types/jwt-decode/-/jwt-decode-2.2.1.tgz#afdf5c527fcfccbd4009b5fd02d1e18241f2d2f2"
integrity sha512-aWw2YTtAdT7CskFyxEX2K21/zSDStuf/ikI3yBqmwpwJF0pS+/IX5DWv+1UFffZIbruP6cnT9/LAJV1gFwAT1A==

"@types/keygrip@*":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@types/keygrip/-/keygrip-1.0.2.tgz#513abfd256d7ad0bf1ee1873606317b33b1b2a72"
Expand Down Expand Up @@ -5493,6 +5498,13 @@ axios@^0.20.0:
dependencies:
follow-redirects "^1.10.0"

axios@^0.21.1:
version "0.21.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.1.tgz#22563481962f4d6bde9a76d516ef0e5d3c09b2b8"
integrity sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==
dependencies:
follow-redirects "^1.10.0"

axobject-query@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-2.2.0.tgz#943d47e10c0b704aa42275e20edf3722648989be"
Expand Down Expand Up @@ -12402,6 +12414,11 @@ jws@^4.0.0:
jwa "^2.0.0"
safe-buffer "^5.0.1"

jwt-decode@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/jwt-decode/-/jwt-decode-2.2.0.tgz#7d86bd56679f58ce6a84704a657dd392bba81a79"
integrity sha1-fYa9VmefWM5qhHBKZX3TkruoGnk=

keyv@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/keyv/-/keyv-3.1.0.tgz#ecc228486f69991e49e9476485a5be1e8fc5c4d9"
Expand Down Expand Up @@ -13782,6 +13799,17 @@ next-tick@~1.0.0:
resolved "https://registry.yarnpkg.com/next-tick/-/next-tick-1.0.0.tgz#ca86d1fe8828169b0120208e3dc8424b9db8342c"
integrity sha1-yobR/ogoFpsBICCOPchCS524NCw=

nhost-js-sdk@^3.0.0-10:
version "3.0.0-10"
resolved "https://registry.yarnpkg.com/nhost-js-sdk/-/nhost-js-sdk-3.0.0-10.tgz#99c8261085910d2848ecaea3638d61dd878dadfe"
integrity sha512-hp7zCT1/7Z4Kb/wMiDqgUi5xHDLOOkkAdeY4IS3qNUh9AcXlPeGHg+6jxvgsQlv+wiR2f8vd1FZZNdCJyrAN4g==
dependencies:
"@types/jwt-decode" "^2.2.1"
axios "^0.21.1"
jwt-decode "^2.2.0"
node-blob "git://github.com/elitan/node-blob.git"
query-string "^6.13.1"

nice-try@^1.0.4:
version "1.0.5"
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
Expand All @@ -13802,6 +13830,10 @@ node-abi@^2.7.0:
dependencies:
semver "^5.4.1"

"node-blob@git://github.com/elitan/node-blob.git":
version "0.0.2"
resolved "git://github.com/elitan/node-blob.git#0382ee22c765fa4c85ae64219fbe4abba1b2a474"

node-dir@^0.1.10:
version "0.1.17"
resolved "https://registry.yarnpkg.com/node-dir/-/node-dir-0.1.17.tgz#5f5665d93351335caabef8f1c554516cf5f1e4e5"
Expand Down Expand Up @@ -15412,6 +15444,15 @@ query-string@^6.12.1:
split-on-first "^1.0.0"
strict-uri-encode "^2.0.0"

query-string@^6.13.1:
version "6.13.8"
resolved "https://registry.yarnpkg.com/query-string/-/query-string-6.13.8.tgz#8cf231759c85484da3cf05a851810d8e825c1159"
integrity sha512-jxJzQI2edQPE/NPUOusNjO/ZOGqr1o2OBa/3M00fU76FsLXDVbJDv/p7ng5OdQyorKrkRz1oqfwmbe5MAMePQg==
dependencies:
decode-uri-component "^0.2.0"
split-on-first "^1.0.0"
strict-uri-encode "^2.0.0"

querystring-es3@^0.2.0:
version "0.2.1"
resolved "https://registry.yarnpkg.com/querystring-es3/-/querystring-es3-0.2.1.tgz#9ec61f79049875707d69414596fd907a4d711e73"
Expand Down

0 comments on commit 601b33a

Please sign in to comment.