diff --git a/src/components/Register.vue b/src/components/Register.vue index 38d5c43..68fff0f 100644 --- a/src/components/Register.vue +++ b/src/components/Register.vue @@ -2,17 +2,14 @@

Register new user

- + - - - - - Register + + + Register Have a user? Sign in here +
@@ -34,57 +31,47 @@ export default { } }, methods: { - requestNewUser(){ - let { username, password, passwordRepeat } = this + submit() { + this.messages = []; + let { username, password, passwordRepeat } = this; - let verifyCredentials = this.checkCredentials(username, password, passwordRepeat); + if (username == null || username.length == 0) { + this.messages.push({ type: 'error', title: 'Missing username' }) + return + } else if (password == null || password.length == 0) { + this.messages.push({ type: 'error', title: 'Missing password' }) + return + } else if (passwordRepeat == null || passwordRepeat.length == 0) { + this.messages.push({ type: 'error', title: 'Missing repeat password' }) + return + } else if (passwordRepeat != password) { + this.messages.push({ type: 'error', title: 'Passwords do not match' }) + return + } - if (verifyCredentials.verified) { + this.registerUser(username, password) + }, + registerUser(username, password) { + register(username, password, true) + .then(data => { + if (data.success){ + localStorage.setItem('token', data.token); + const jwtData = parseJwt(data.token) + localStorage.setItem('username', jwtData['username']); + localStorage.setItem('admin', jwtData['admin'] || false); - register(username, password) - .then(data => { - if (data.success){ - localStorage.setItem('token', data.token); - localStorage.setItem('username', username); - localStorage.setItem('admin', data.admin) - - eventHub.$emit('setUserStatus'); - this.$router.push({ name: 'profile' }) - } + eventHub.$emit('setUserStatus'); + this.$router.push({ name: 'profile' }) + } }) .catch(error => { - this.messages.push({ type: 'error', title: 'Unexpected error', message: error.message }) + if (error.status === 401) { + this.messages.push({ type: 'error', title: 'Access denied', message: 'Incorrect username or password' }) + } + else { + this.messages.push({ type: 'error', title: 'Unexpected error', message: error.message }) + } }); - } - else { - this.messages.push({ type: 'warning', title: 'Parse error', message: verifyCredentials.reason }) - } - }, - checkCredentials(username, password, passwordRepeat) { - if (!username || username.length === 0) { - return { - verified: false, - reason: 'Fill inn username' - } - } - else if (!password || !passwordRepeat) { - return { - verified: false, - reason: "Fill inn both password fields" - } - } - else if (password !== passwordRepeat) { - return { - verified: false, - reason: 'Passwords do not match' - } - } - else { - return { - verified: true, - reason: 'Verified credentials' - } - } }, logOut(){ localStorage.clear(); diff --git a/src/components/Signin.vue b/src/components/Signin.vue index 4ca3969..8baea18 100644 --- a/src/components/Signin.vue +++ b/src/components/Signin.vue @@ -5,14 +5,14 @@ - - - sign in + + sign in Don't have a user? Register here - + @@ -39,11 +39,25 @@ export default { setValue(l, t) { this[l] = t }, - signin(){ + submit() { + this.messages = []; let username = this.username; let password = this.password; - login(username, password) + if (username == null || username.length == 0) { + this.messages.push({ type: 'error', title: 'Missing username' }) + return + } + + if (password == null || password.length == 0) { + this.messages.push({ type: 'error', title: 'Missing password' }) + return + } + + this.signin(username, password) + }, + signin(username, password) { + login(username, password, true) .then(data => { if (data.success){ const jwtData = parseJwt(data.token) @@ -57,7 +71,7 @@ export default { }) .catch(error => { if (error.status === 401) { - this.messages.push({ type: 'warning', title: 'Access denied', message: 'Incorrect username or password' }) + this.messages.push({ type: 'error', title: 'Access denied', message: 'Incorrect username or password' }) } else { this.messages.push({ type: 'error', title: 'Unexpected error', message: error.message })