Skip to content

Commit

Permalink
Merge branch 'feat/auth-optional-get-token'
Browse files Browse the repository at this point in the history
  • Loading branch information
Pagebakers committed Sep 20, 2024
2 parents 5e000e0 + d957730 commit 069de14
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 52 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/pkg-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Package PR

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- run: corepack enable
- uses: actions/setup-node@v4
with:
node-version: 20

- name: Install dependencies
run: YARN_ENABLE_IMMUTABLE_INSTALLS=false yarn

- name: Build
run: yarn build:packages

- run: yarn dlx pkg-pr-new publish './packages/saas-ui-auth'
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn lint:staged
# yarn lint:staged
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"changelog:commit": "git add CHANGELOG.md && git commit -am 'docs: add changelog' && git push",
"changeset:next": "yarn changeset pre enter next",
"changeset:version": "yarn changelog:gen && changeset version",
"publish:next": "yarn workspaces foreach --from '@saas-ui/*' --exclude '@saas-ui/pro' --exclude '@saas-ui-pro/*' npm publish --access public --tag next",
"publish:next": "yarn workspaces foreach -Wp --from '@saas-ui/*' --exclude '@saas-ui/pro' --exclude '@saas-ui-pro/*' npm publish --access public --tag next",
"publish": "yarn workspaces foreach -Wp --from '@saas-ui/*' --exclude '@saas-ui/pro' --exclude '@saas-ui-pro/*' npm publish && yarn changeset tag",
"storybook": "yarn w saas-ui-storybook dev",
"build:storybook": "yarn w saas-ui-storybook build",
Expand Down
12 changes: 12 additions & 0 deletions packages/saas-ui-auth/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# @saas-ui/auth

## 3.3.0

### Minor Changes

- c8c696e: onGetToken no longer required for authentication service

## 3.3.0-next.0

### Minor Changes

- c8c696e: onGetToken no longer required for authentication service

## 3.2.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/saas-ui-auth/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@saas-ui/auth",
"version": "3.2.2",
"version": "3.3.0",
"description": "Authentication provider and components for Chakra UI",
"source": "src/index.ts",
"exports": {
Expand Down
63 changes: 28 additions & 35 deletions packages/saas-ui-auth/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export interface AuthContextValue<TUser extends User = DefaultUser> {
updatePassword: AuthFunction<UpdatePasswordParams>
logOut: (options?: AuthOptions) => Promise<unknown>
loadUser: () => void
getToken: () => Promise<AuthToken>
getToken?: () => Promise<AuthToken>
}

const createAuthContext = <TUser extends User = DefaultUser>() => {
Expand Down Expand Up @@ -190,61 +190,54 @@ export const AuthProvider = <TUser extends User = DefaultUser>({
}
}, [onRestoreAuthState])

const checkAuth = useCallback(async () => {
try {
if (onGetToken) {
const isAuthenticated = !!(await onGetToken())
setAuthenticated(isAuthenticated)
return isAuthenticated
}
} catch (e) {
setAuthenticated(false)
}
}, [onGetToken])

useEffect(() => {
if (!refetchUserOnWindowFocus) {
return
}

const onWindowFocus = async () => {
loadUser()
}

window.addEventListener('focus', onWindowFocus)

return () => {
if (onWindowFocus) {
window.removeEventListener('focus', onWindowFocus)
}
}
}, [checkAuth, refetchUserOnWindowFocus])

const loadUser = useCallback(async () => {
try {
// Prevent multiple calls to loadUser
if (isFetchingRef.current) {
return
}

const isAuthenticated = await checkAuth()

if (isAuthenticated && !isFetchingRef.current) {
if (
(typeof onGetToken === 'undefined' || (await onGetToken())) &&
!isFetchingRef.current
) {
isFetchingRef.current = true
const user = await onLoadUser()

if (user) {
setUser(user)
setAuthenticated(true)
} else {
setAuthenticated(false)
setUser(null)
}
}
} catch {
setAuthenticated(false)
setUser(null)
} finally {
isFetchingRef.current = false
setLoading(false)
}
}, [onLoadUser, checkAuth])
}, [onLoadUser, onGetToken])

useEffect(() => {
if (!refetchUserOnWindowFocus) {
return
}

const onWindowFocus = async () => {
loadUser()
}

window.addEventListener('focus', onWindowFocus)

return () => {
if (onWindowFocus) {
window.removeEventListener('focus', onWindowFocus)
}
}
}, [loadUser, refetchUserOnWindowFocus])

const signUp = useCallback(
async (params: AuthParams, options?: AuthOptions) => {
Expand Down
5 changes: 0 additions & 5 deletions packages/saas-ui-auth/stories/auth.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ const createAuthService = (): AuthProviderProps<CustomUser> => {
console.log('onLoadUser', user)
return user
},
onGetToken: async () => {
// return a session token if it's supported.
console.log('onGetToken', user?.email)
return user?.email
},
onResetPassword: async (params: AuthParams) => {
// send a reset password email
},
Expand Down
14 changes: 14 additions & 0 deletions packages/saas-ui-auth0/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# @saas-ui/auth0

## 0.1.13

### Patch Changes

- Updated dependencies [c8c696e]
- @saas-ui/auth@3.3.0

## 0.1.13-next.0

### Patch Changes

- Updated dependencies [c8c696e]
- @saas-ui/auth@3.3.0-next.0

## 0.1.12

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/saas-ui-auth0/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@saas-ui/auth0",
"version": "0.1.12",
"version": "0.1.13",
"description": "Auth0 authentication for Saas UI",
"source": "src/index.ts",
"exports": {
Expand Down
14 changes: 14 additions & 0 deletions packages/saas-ui-clerk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# @saas-ui/clerk

## 3.0.49

### Patch Changes

- Updated dependencies [c8c696e]
- @saas-ui/auth@3.3.0

## 3.0.49-next.0

### Patch Changes

- Updated dependencies [c8c696e]
- @saas-ui/auth@3.3.0-next.0

## 3.0.48

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/saas-ui-clerk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@saas-ui/clerk",
"version": "3.0.48",
"version": "3.0.49",
"description": "Clerk Authentication Service for Saas UI",
"source": "src/index.ts",
"exports": {
Expand Down
14 changes: 14 additions & 0 deletions packages/saas-ui-magic/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# @saas-ui/auth

## 2.1.3

### Patch Changes

- Updated dependencies [c8c696e]
- @saas-ui/auth@3.3.0

## 2.1.3-next.0

### Patch Changes

- Updated dependencies [c8c696e]
- @saas-ui/auth@3.3.0-next.0

## 2.1.2

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/saas-ui-magic/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@saas-ui/magic",
"version": "2.1.2",
"version": "2.1.3",
"description": "Saas UI Magic.link integration",
"source": "src/index.ts",
"exports": {
Expand Down Expand Up @@ -55,7 +55,7 @@
"magic.link"
],
"dependencies": {
"@saas-ui/auth": "^3.0.0"
"@saas-ui/auth": "^3.3.0"
},
"peerDependencies": {
"@chakra-ui/react": ">=2.4.9",
Expand Down
14 changes: 14 additions & 0 deletions packages/saas-ui-supabase/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# @saas-ui/supabase

## 2.2.1

### Patch Changes

- Updated dependencies [c8c696e]
- @saas-ui/auth@3.3.0

## 2.2.1-next.0

### Patch Changes

- Updated dependencies [c8c696e]
- @saas-ui/auth@3.3.0-next.0

## 2.2.0

### Minor Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/saas-ui-supabase/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@saas-ui/supabase",
"version": "2.2.0",
"version": "2.2.1",
"description": "Saas UI Supabase integration",
"source": "src/index.ts",
"exports": {
Expand Down Expand Up @@ -59,7 +59,7 @@
"url": "https://storybook.saas-ui.dev"
},
"dependencies": {
"@saas-ui/auth": "^3.2.0"
"@saas-ui/auth": "^3.3.0"
},
"peerDependencies": {
"@chakra-ui/react": ">=2.4.9",
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7326,7 +7326,7 @@ __metadata:
languageName: unknown
linkType: soft

"@saas-ui/auth@npm:^3.0.0, @saas-ui/auth@npm:^3.2.0, @saas-ui/auth@workspace:*, @saas-ui/auth@workspace:packages/saas-ui-auth":
"@saas-ui/auth@npm:^3.3.0-next.0, @saas-ui/auth@workspace:*, @saas-ui/auth@workspace:packages/saas-ui-auth":
version: 0.0.0-use.local
resolution: "@saas-ui/auth@workspace:packages/saas-ui-auth"
dependencies:
Expand Down Expand Up @@ -7553,7 +7553,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@saas-ui/magic@workspace:packages/saas-ui-magic"
dependencies:
"@saas-ui/auth": "npm:^3.0.0"
"@saas-ui/auth": "npm:^3.3.0-next.0"
magic-sdk: "npm:^17.4.0"
peerDependencies:
"@chakra-ui/react": ">=2.4.9"
Expand Down Expand Up @@ -7695,7 +7695,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@saas-ui/supabase@workspace:packages/saas-ui-supabase"
dependencies:
"@saas-ui/auth": "npm:^3.2.0"
"@saas-ui/auth": "npm:^3.3.0-next.0"
"@supabase/supabase-js": "npm:^2.45.4"
peerDependencies:
"@chakra-ui/react": ">=2.4.9"
Expand Down

0 comments on commit 069de14

Please sign in to comment.