|
1 | 1 | # useSignIn |
2 | 2 |
|
| 3 | +Log in existing user, or login via a third-party provider. |
| 4 | + |
| 5 | +```js |
| 6 | +import { useSignIn } from 'react-supabase' |
| 7 | + |
| 8 | +function Page() { |
| 9 | + const [{ error, fetching, session, user }, signIn] = useSignIn({ |
| 10 | + // Passing optional options |
| 11 | + options: { |
| 12 | + redirectTo: 'https://example.com/dashboard', |
| 13 | + }, |
| 14 | + }) |
| 15 | + |
| 16 | + async function onClickSignIn() { |
| 17 | + const { error, session, user } = await signIn( |
| 18 | + { |
| 19 | + email: 'user@example.com', |
| 20 | + password: 'foobarbaz', |
| 21 | + }, |
| 22 | + { |
| 23 | + // Override options from hook init |
| 24 | + redirectTo: 'https://example.com/account', |
| 25 | + }, |
| 26 | + ) |
| 27 | + } |
| 28 | + |
| 29 | + if (error) return <div>Error signing in</div> |
| 30 | + if (fetching) return <div>Signing in</div> |
| 31 | + if (user) return <div>Logged in</div> |
| 32 | + |
| 33 | + return ... |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +## Magic links |
| 38 | + |
| 39 | +Omit password from the execute function: |
| 40 | + |
| 41 | +```js |
| 42 | +const { error, session, user } = await signIn({ email: 'user@example.com' }) |
| 43 | +``` |
| 44 | + |
| 45 | +## Third-party providers |
| 46 | + |
| 47 | +Either pass a provider (and scopes) during hook initialization: |
| 48 | + |
3 | 49 | ```js |
4 | 50 | const [{ error, fetching, user, session }, signIn] = useSignIn({ |
5 | | - // provider: 'github', |
| 51 | + provider: 'github', |
6 | 52 | options: { |
7 | | - redirectTo: 'https://example.com/dashboard', |
8 | | - // scopes: 'repo gist notifications', |
| 53 | + scopes: 'repo gist notifications', |
9 | 54 | }, |
10 | 55 | }) |
| 56 | +``` |
| 57 | + |
| 58 | +Or the execute function: |
11 | 59 |
|
| 60 | +```js |
12 | 61 | const { error, session, user } = await signIn( |
13 | | - { |
14 | | - email: 'user@example.com', |
15 | | - password: 'foobarbaz', // omit for magic link |
16 | | - // provider: 'github', |
17 | | - }, |
18 | | - { |
19 | | - redirectTo: 'https://example.com/dashboard', |
20 | | - // scopes: 'repo gist notifications', |
21 | | - }, |
| 62 | + { provider: 'github' }, |
| 63 | + { scopes: 'repo gist notifications' }, |
22 | 64 | ) |
23 | 65 | ``` |
0 commit comments