Skip to content

Commit 04e68d7

Browse files
committed
feat: add auth hooks
1 parent ae0cb2b commit 04e68d7

31 files changed

+1020
-30
lines changed

.config/husky/commit-msg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/sh
22
. "$(dirname "$0")/_/husky.sh"
33

4-
npx commitlint --config .config/.commitlintrc --edit $1
4+
yarn commitlint --config .config/.commitlintrc --edit $1

.config/husky/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/sh
22
. "$(dirname "$0")/_/husky.sh"
33

4-
npx lint-staged --config .config/.lintstagedrc
4+
yarn lint-staged --config .config/.lintstagedrc

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"sourceType": "module",
77
"ecmaFeatures": {
88
"impliedStrict": true,
9-
"experimentalObjectRestSpread": true,
9+
"experimentalObjectRestSpread": true
1010
},
1111
"allowImportExportEverywhere": true
1212
},

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
"tabWidth": 4,
88
"trailingComma": "all",
99
"overrides": [
10+
{
11+
"files": "*.md",
12+
"options": {
13+
"tabWidth": 2
14+
}
15+
},
1016
{
1117
"files": "*.yaml",
1218
"options": {

README.md

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,55 @@
44

55
<br/>
66

7-
## Usage
7+
## Installation
88

9-
Inside your React project directory, run the following:
9+
Inside your React project directory, install:
1010

1111
```
12-
yarn add react-supabase
12+
yarn add react-supabase @supabase/supabase-js
13+
# or
14+
npm install --save react-supabase @supabase/supabase-js
1315
```
1416

15-
Or with npm:
17+
<br/>
18+
19+
## Usage
20+
21+
Create a Supabase client and pass it to the `Provider`:
22+
23+
```js
24+
import { createClient } from '@supabase/supabase-js'
25+
import { Provider } from 'react-supabase'
1626

27+
const client = createClient('https://xyzcompany.supabase.co', 'public-anon-key')
28+
29+
const App = () => (
30+
<Provider value={client}>
31+
<YourRoutes />
32+
</Provider>
33+
)
1734
```
18-
npm install react-supabase
35+
36+
Now every component and element inside and under the `Provider` can use the Supabase client:
37+
38+
```js
39+
import { useSelect } from 'react-supabase'
40+
41+
const Todos = () => {
42+
const [result, reexecuteSelect] = useSelect('todos')
43+
44+
const { data, fetching, error } = result
45+
46+
if (fetching) return <p>Loading...</p>
47+
if (error) return <p>Oh no... {error.message}</p>
48+
return (
49+
<ul>
50+
{data.map((todo) => (
51+
<li key={todo.id}>{todo.title}</li>
52+
))}
53+
</ul>
54+
)
55+
}
1956
```
2057

2158
<br/>

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
preset: 'ts-jest',
3-
testRegex: '/test/.*\\.test\\.tsx$',
3+
testRegex: '.*\\.test\\.(ts|tsx)$',
44
watchPlugins: [
55
'jest-watch-typeahead/filename',
66
'jest-watch-typeahead/testname',

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@types/react": "^17.0.3",
3838
"@typescript-eslint/eslint-plugin": "^4.22.0",
3939
"@typescript-eslint/parser": "^4.22.0",
40+
"commitlint": "^12.1.1",
4041
"concurrently": "^6.0.2",
4142
"eslint": "^7.24.0",
4243
"eslint-config-prettier": "^8.2.0",
@@ -50,6 +51,7 @@
5051
"husky": "^6.0.0",
5152
"jest": "^26.6.3",
5253
"jest-watch-typeahead": "^0.6.3",
54+
"lint-staged": "^10.5.4",
5355
"prettier": "^2.2.1",
5456
"react": "^17.0.2",
5557
"react-dom": "^17.0.2",

src/hooks/auth/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export * from './use-auth-state-change'
2+
export * from './use-reset-password'
3+
export * from './use-signin'
4+
export * from './use-signout'
5+
export * from './use-signup'

src/hooks/auth/state.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const initialState = {
2+
error: undefined,
3+
fetching: false,
4+
session: undefined,
5+
user: undefined,
6+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { AuthChangeEvent, Session } from '@supabase/gotrue-js'
2+
import { useEffect } from 'react'
3+
4+
import { useClient } from '../use-client'
5+
6+
export function useAuthStateChange(
7+
callback: (event: AuthChangeEvent, session: Session | null) => void,
8+
) {
9+
const client = useClient()
10+
11+
/* eslint-disable react-hooks/exhaustive-deps */
12+
useEffect(() => {
13+
const { data: authListener } = client.auth.onAuthStateChange(callback)
14+
return () => {
15+
authListener?.unsubscribe()
16+
}
17+
}, [])
18+
/* eslint-enable react-hooks/exhaustive-deps */
19+
}

0 commit comments

Comments
 (0)