-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlogin.vue
162 lines (151 loc) · 4.16 KB
/
login.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<template>
<NuxtLayout name="bare">
<template #side>
<div class="flex flex-col">
<img
class="w-11/12 mx-auto"
src="~/assets/undraw_book_lover_mkck.svg"
/>
<div class="mt-7 mx-auto text-2xl">Stay on top of your literature!</div>
</div>
</template>
<div>
<h2 class="text-center text-5xl font-extrabold text-gray-900">Sign in</h2>
<p class="mt-6 mb-8 text-center text-sm text-gray-600">
Don't have an account?
<t-nuxtlink to="/user/register">Sign up</t-nuxtlink>
</p>
<n-alert
v-if="error"
type="error"
class="mb-4"
>
{{ error }}
</n-alert>
<n-form
size="lg"
@submit="onSubmit"
>
<div class="space-y-2">
<n-form-item
label="Email address"
label-style="font-weight: 600"
:feedback="errors.email"
:validation-status="errors.email ? 'error' : undefined"
>
<n-input
v-model:value="values.email"
v-focus
/>
</n-form-item>
<n-form-item
label="Password"
label-style="font-weight: 600"
:feedback="errors.password"
:validation-status="errors.password ? 'error' : undefined"
>
<n-input
v-model:value="values.password"
type="password"
show-password-on="mousedown"
/>
</n-form-item>
<div class="flex items-center justify-between">
<div class="flex items-center">
<n-checkbox v-model:checked="rememberLogin">
Keep me logged in
</n-checkbox>
</div>
<div class="text-sm">
<t-nuxtlink to="./forgot-password"
>Forgot your password?</t-nuxtlink
>
</div>
</div>
<div class="py-2 text-center">
<UButton
class="w-full"
type="primary"
attr-type="submit"
>Sign in</UButton
>
</div>
<div>
<HorizontalRule content="or sign in with" />
</div>
<div class="pt-2 flex justify-center">
<img
id="orcidLogoFooter"
class="w-28"
src="~/assets/ORCID_logo.svg"
alt="ORCID"
/>
</div>
</div>
</n-form>
</div>
</NuxtLayout>
</template>
<script lang="ts" setup>
import { useMutation } from '@vue/apollo-composable'
import { gql } from '~/apollo'
import { cacheCurrentUser } from '~/apollo/cache'
import { LoginInputSchema } from '~/apollo/validation'
definePageMeta({ layout: false })
// TODO: Automatically go to home if already logged in
// middleware: 'guest',
const { handleSubmit, errors, values } = useForm({
validationSchema: toTypedSchema(LoginInputSchema),
})
const otherError = ref('')
const {
mutate: loginUser,
onDone,
error: graphqlError,
} = useMutation(
gql(/* GraphQL */ `
mutation Login($input: LoginInput!) {
login(input: $input) {
... on UserReturned {
user {
id
}
}
... on InputValidationProblem {
problems {
path
message
}
}
}
}
`),
{
update(cache, { data: login }) {
if (login?.login?.__typename === 'UserReturned') {
const { user } = login.login
cacheCurrentUser(cache, user)
} else {
cacheCurrentUser(cache, null)
}
},
},
)
onDone((result) => {
if (result.data?.login?.__typename === 'UserReturned') {
void navigateTo({ name: 'dashboard' })
} else {
otherError.value =
result.data?.login?.__typename === 'InputValidationProblem' &&
result.data.login.problems[0]
? result.data.login.problems[0].message
: 'Unknown error'
}
})
const error = computed(() => graphqlError.value ?? otherError.value)
// TODO: Implement remember login
const rememberLogin = ref(false)
const onSubmit = handleSubmit(async (values) => {
await loginUser({ input: values })
})
</script>