Skip to content

Commit 5c710b4

Browse files
Merge pull request #20 from CyberCowboy404/main
adds anonymous user support
2 parents 6184a84 + ce71115 commit 5c710b4

File tree

6 files changed

+2428
-1921
lines changed

6 files changed

+2428
-1921
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Customizable authentication UI component with custom themes and extensible style
3636
- [Social Providers](#social-providers)
3737
- [Options](#options)
3838
- [Supported Views](#supported-views)
39+
- [Anonymous User](#anonymous-user)
3940
- [Customization](#customization)
4041
- [Predefined themes](#predefined-themes)
4142
- [Switch theme variations](#switch-theme-variations)
@@ -229,9 +230,22 @@ const redirectTo = computed(() => {
229230
return authView.value === 'forgotten_password' ? FORGOTTEN_PASSWORD_URL : REDIRECT_TO_URL
230231
})
231232
</script>
233+
234+
```
235+
### Anonymous User
236+
237+
The Auth component is currently support [Anonymous user login](https://supabase.com/docs/guides/auth/auth-anonymous#sign-in-anonymously).
238+
For this you need to create an anonymous user
239+
240+
```js
241+
const { data, error } = await supabase.auth.signInAnonymously()
232242
```
233243

234-
We are planning on adding more views in the future. Follow along on this repo.
244+
[Enable manual linking](https://supabase.com/dashboard/project/_/settings/auth) in supabase
245+
246+
Currently works for magic link and social login.
247+
If you use password login, you need user to update password after he
248+
235249

236250
## Customization
237251

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@
4444
},
4545
"peerDependencies": {
4646
"@supabase/auth-ui-shared": "^0.1.8",
47-
"@supabase/supabase-js": "^2.38.5"
47+
"@supabase/supabase-js": "^2.42.7"
4848
},
4949
"devDependencies": {
5050
"@iconify/json": "^2.1.104",
5151
"@intlify/unplugin-vue-i18n": "^0.12.2",
5252
"@stitches/core": "^1.2.8",
5353
"@supabase/auth-ui-shared": "^0.1.8",
54-
"@supabase/supabase-js": "^2.38.5",
54+
"@supabase/supabase-js": "^2.42.7",
5555
"@types/lodash.clonedeep": "^4.5.7",
5656
"@types/node": "^18.7.14",
5757
"@unocss/reset": "^0.45.18",

packages/auth/MagicLink.vue

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import { I18nVariables, RedirectTo, VIEWS } from '@supabase/auth-ui-shared'
6060
import { AuthViewKey, type Appearance, type AuthViewInjection } from '../types'
6161
import { Anchor, Button, Container, Input, Label, Message } from '../ui/index'
6262
import { injectStrict } from '../utils'
63+
import { useSupabaseUser } from './UserContextProvider'
6364
6465
export interface MagicLinkProps {
6566
appearance?: Appearance
@@ -71,6 +72,8 @@ export interface MagicLinkProps {
7172
7273
const props = withDefaults(defineProps<MagicLinkProps>(), {})
7374
75+
const { supabaseUser } = useSupabaseUser(props.supabaseClient)
76+
7477
const email = ref('')
7578
const error = ref('')
7679
const message = ref('')
@@ -87,10 +90,23 @@ const handleSubmit = async (e: Event) => {
8790
error.value = ''
8891
message.value = ''
8992
isLoading.value = true
90-
const { error: signInError } = await props.supabaseClient.auth.signInWithOtp({
91-
email: email.value,
92-
options: { emailRedirectTo: props.redirectTo }
93-
})
93+
const isAnonymous = supabaseUser.value?.is_anonymous
94+
let signInError: Error | null = null
95+
if (isAnonymous) {
96+
const { error: err } = await props.supabaseClient.auth.updateUser({
97+
email: email.value
98+
}, {
99+
emailRedirectTo: props.redirectTo
100+
})
101+
signInError = err
102+
} else {
103+
const { error: err } = await props.supabaseClient.auth.signInWithOtp({
104+
email: email.value,
105+
options: { emailRedirectTo: props.redirectTo }
106+
})
107+
signInError = err
108+
}
109+
94110
if (signInError) {
95111
error.value = signInError.message
96112
} else {

packages/auth/SocialAuth.vue

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import { Appearance, AuthViewInjection, AuthViewKey } from '../types'
4848
import { Divider, Button, Container } from '@/ui'
4949
import { Icons } from '@/icons'
5050
import { injectStrict } from '../utils'
51+
import { useSupabaseUser } from './UserContextProvider'
5152
5253
type RedirectTo = undefined | string
5354
@@ -71,6 +72,8 @@ const props = withDefaults(defineProps<SocialAuthProps>(), {
7172
view: 'sign_in'
7273
})
7374
75+
const { supabaseUser } = useSupabaseUser(props.supabaseClient)
76+
7477
const error = ref('')
7578
const isLoading = ref(false)
7679
const { authView } = injectStrict<AuthViewInjection>(AuthViewKey)
@@ -90,16 +93,32 @@ const labels = computed(
9093
const handleProviderSignIn = async (provider: Provider) => {
9194
error.value = ''
9295
isLoading.value = true
93-
const { data, error: err } = await props.supabaseClient.auth.signInWithOAuth({
94-
provider,
95-
options: {
96-
redirectTo: props.redirectTo,
97-
scopes: props.providerScopes?.[provider],
98-
queryParams: props.queryParams
99-
}
100-
})
96+
97+
const isAnonymous = supabaseUser.value?.is_anonymous
98+
const options = {
99+
redirectTo: props.redirectTo,
100+
scopes: props.providerScopes?.[provider],
101+
queryParams: props.queryParams
102+
}
103+
104+
let signInError: Error | null = null
105+
106+
if (isAnonymous) {
107+
const { data, error: err } = await props.supabaseClient.auth.linkIdentity({
108+
provider,
109+
options
110+
})
111+
signInError = err
112+
} else {
113+
const { data, error: err } = await props.supabaseClient.auth.signInWithOAuth({
114+
provider,
115+
options
116+
})
117+
signInError = err
118+
}
119+
101120
// console.log(data)
102-
if (err) error.value = err.message
121+
if (signInError) error.value = signInError.message
103122
isLoading.value = false
104123
}
105124

0 commit comments

Comments
 (0)