Skip to content
This repository was archived by the owner on May 7, 2021. It is now read-only.

Commit 577a2db

Browse files
authored
Merge pull request #65 from ProgrammingLab/kp-user-registration
ユーザー登録
2 parents 9ec7afa + 4a90814 commit 577a2db

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

src/api/user/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,16 @@ export default {
66
const userData = (await api.client.get('/user', getHeader(sessionID))).data;
77
return userData;
88
},
9+
// eslint-disable-next-line camelcase
10+
async registerUser(name, full_name, password, registration_token) {
11+
const ret = (await api.client.post('/users', {
12+
name, full_name, password, registeration_token: registration_token,
13+
})).data;
14+
return ret;
15+
},
16+
17+
async verifyRegistrationToken(token) {
18+
const ret = (await api.client.get(`/invitations/${token}`)).data;
19+
return ret;
20+
},
921
};

src/router.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ const router = new Router({
4343
// which is lazy-loaded when the route is visited.
4444
component: () => import(/* webpackChunkName: "oauthConsent" */ './views/oauth/Consent.vue'),
4545
},
46+
{
47+
path: '/registration/:token',
48+
name: 'registration',
49+
component: () => import(/* webpackChunkName: "registration" */ './views/Registration.vue'),
50+
},
4651
],
4752
});
4853

src/store/modules/user/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,50 @@ export default {
44
namespaced: true,
55
state: {
66
userData: null,
7+
registrationError: null,
8+
tokenVerificationError: null,
79
},
810
/* eslint-disable no-param-reassign */
911
mutations: {
1012
setUser(state, user) {
1113
state.userData = user;
1214
},
15+
setRegistrationError(state, error) {
16+
state.registrationError = error;
17+
},
18+
clearRegistrationError(state) {
19+
state.registrationError = null;
20+
},
21+
setTokenVerificationError(state, error) {
22+
state.tokenVerificationError = error;
23+
},
24+
clearTokenVerificationError(state) {
25+
state.tokenVerificationError = null;
26+
},
1327
},
1428
/* eslint-enable no-param-reassign */
1529
actions: {
1630
async getUser({ commit }, sessionID) {
1731
commit('setUser', await userClient.getUser(sessionID));
1832
},
33+
async verifyRegistrationToken({ commit }, token) {
34+
commit('clearTokenVerificationError');
35+
try {
36+
await userClient.verifyRegistrationToken(token);
37+
} catch (e) {
38+
commit('setTokenVerificationError', e);
39+
}
40+
},
41+
async register({ commit }, {
42+
// eslint-disable-next-line camelcase
43+
name, full_name, password, registration_token,
44+
}) {
45+
commit('clearRegistrationError');
46+
try {
47+
await userClient.registerUser(name, full_name, password, registration_token);
48+
} catch (e) {
49+
commit('setRegistrationError', e);
50+
}
51+
},
1952
},
2053
};

src/views/Registration.vue

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<template>
2+
<div class="container">
3+
<div class="box">
4+
<form v-on:submit.prevent='onRegister'>
5+
<label for="username">User Name</label>
6+
<input required type="text" id="username" v-model="name">
7+
<label for="full_name">Full Name</label>
8+
<input required type="text" id="full_name" v-model="full_name">
9+
<label for="password">Password</label>
10+
<input required type="password" id="password" v-model="password">
11+
<label for="password_confirm">Confirm Password</label>
12+
<input required type="password" id="password_confirm" v-model="password_confirm">
13+
<div class="errorMessage" v-if="passwordConfirmationError">パスワードが一致しません</div>
14+
<button type="submit">Register</button>
15+
<ErrorMessage :error="registrationError"/>
16+
</form>
17+
</div>
18+
</div>
19+
</template>
20+
21+
<script>
22+
import { mapMutations, mapActions, mapState } from 'vuex';
23+
import ErrorMessage from '@/components/ErrorMessage.vue';
24+
25+
export default {
26+
name: 'registration',
27+
components: {
28+
ErrorMessage,
29+
},
30+
data() {
31+
return {
32+
name: '',
33+
full_name: '',
34+
password: '',
35+
password_confirm: '',
36+
passwordConfirmationError: false,
37+
registration_token: '',
38+
};
39+
},
40+
computed: {
41+
...mapState('user', [
42+
'tokenVerificationError',
43+
'registrationError',
44+
]),
45+
...mapState('session', [
46+
'loginError',
47+
]),
48+
},
49+
async created() {
50+
this.registration_token = this.$route.params.token;
51+
await this.verifyRegistrationToken(this.registration_token);
52+
if (this.tokenVerificationError) {
53+
this.createError({
54+
number: this.tokenVerificationError.response.status,
55+
message: this.tokenVerificationError.response.data.message,
56+
});
57+
}
58+
},
59+
methods: {
60+
...mapMutations('criticalError', [
61+
'createError',
62+
]),
63+
...mapActions('user', [
64+
'verifyRegistrationToken',
65+
'register',
66+
]),
67+
...mapActions('session', [
68+
'login',
69+
]),
70+
async onRegister() {
71+
this.passwordConfirmationError = false;
72+
if (this.password !== this.password_confirm) {
73+
this.passwordConfirmationError = true;
74+
return;
75+
}
76+
await this.register({
77+
name: this.name,
78+
full_name: this.full_name,
79+
password: this.password,
80+
registration_token: this.registration_token,
81+
});
82+
if (!this.registrationError) {
83+
await this.login({ name: this.name, password: this.password });
84+
if (this.loginError) {
85+
this.$router.push({ name: 'login' });
86+
} else {
87+
this.$router.push({ name: 'home' });
88+
}
89+
}
90+
},
91+
},
92+
};
93+
</script>
94+
95+
<style scoped>
96+
.container {
97+
display: flex;
98+
justify-content: center;
99+
align-items: center;
100+
flex-direction: column;
101+
height: 100%;
102+
}
103+
104+
.box {
105+
margin: 0 2em;
106+
}
107+
108+
input[type="text"], input[type="password"] {
109+
width: 100%;
110+
padding: 10px;
111+
border-width: 0 0 2px;
112+
margin-bottom: 20px;
113+
}
114+
115+
button[type="submit"] {
116+
background-color: transparent;
117+
border: none;
118+
cursor: pointer;
119+
outline: none;
120+
padding: 0;
121+
appearance: none;
122+
display: inline-block;
123+
padding: 0.5em 1em;
124+
text-decoration: none;
125+
background: #668ad8;
126+
color: #FFF;
127+
border-radius: 3px;
128+
}
129+
130+
.errorMessage {
131+
color: red;
132+
}
133+
</style>

0 commit comments

Comments
 (0)