|
1 | 1 | <template> |
2 | 2 | <div class="content"> |
3 | | - <h2>Login</h2> |
4 | | - <div class="field is-horizontal"> |
5 | | - <div class="field-label is-normal"> |
6 | | - <label class="label">Username</label> |
7 | | - </div> |
8 | | - <div class="field-body"> |
9 | | - <div class="field"> |
10 | | - <div class="control"> |
11 | | - <input class="input" type="text" placeholder="Your username"> |
12 | | - </div> |
13 | | - </div> |
14 | | - </div> |
15 | | - </div> |
16 | | - <div class="field is-horizontal"> |
17 | | - <div class="field-label is-normal"> |
18 | | - <label class="label">Password</label> |
19 | | - </div> |
20 | | - <div class="field-body"> |
21 | | - <div class="field"> |
22 | | - <div class="control"> |
23 | | - <input class="input" type="password" placeholder="Your password"> |
24 | | - </div> |
25 | | - </div> |
26 | | - </div> |
27 | | - </div> |
28 | | - <div class="field is-horizontal"> |
29 | | - <div class="field-label"> |
30 | | - <!-- Left empty for spacing --> |
31 | | - </div> |
32 | | - <div class="field-body"> |
33 | | - <div class="field"> |
34 | | - <div class="control"> |
35 | | - <button class="button is-primary">Login</button> |
36 | | - </div> |
37 | | - </div> |
38 | | - </div> |
39 | | - </div> |
| 3 | + <div v-if="isAuthenticated"> |
| 4 | + Hello authenticated user! |
| 5 | + <p>Name: {{profile.firstName}}</p> |
| 6 | + <p>Favorite Sandwich: {{profile.favoriteSandwich}}</p> |
| 7 | + <button v-on:click="logout" class="button is-primary">Logout</button> |
| 8 | + </div> |
| 9 | + <div v-else> |
| 10 | + <h2>Login</h2> |
| 11 | + <div class="field is-horizontal"> |
| 12 | + <div class="field-label is-normal"> |
| 13 | + <label class="label">Username</label> |
| 14 | + </div> |
| 15 | + <div class="field-body"> |
| 16 | + <div class="field"> |
| 17 | + <div class="control"> |
| 18 | + <input v-model="username" class="input" type="text" placeholder="Your username"> |
| 19 | + </div> |
| 20 | + </div> |
| 21 | + </div> |
| 22 | + </div> |
| 23 | + <div class="field is-horizontal"> |
| 24 | + <div class="field-label is-normal"> |
| 25 | + <label class="label">Password</label> |
| 26 | + </div> |
| 27 | + <div class="field-body"> |
| 28 | + <div class="field"> |
| 29 | + <div class="control"> |
| 30 | + <input v-model="password" class="input" type="password" placeholder="Your password"> |
| 31 | + </div> |
| 32 | + </div> |
| 33 | + </div> |
| 34 | + </div> |
| 35 | + <div class="field is-horizontal"> |
| 36 | + <div class="field-label"> |
| 37 | + <!-- Left empty for spacing --> |
| 38 | + </div> |
| 39 | + <div class="field-body"> |
| 40 | + <div class="field"> |
| 41 | + <div class="control"> |
| 42 | + <button v-on:click="login" class="button is-primary">Login</button> |
| 43 | + </div> |
| 44 | + </div> |
| 45 | + </div> |
| 46 | + </div> |
| 47 | + </div> |
40 | 48 | </div> |
41 | 49 | </template> |
| 50 | + |
| 51 | +<script> |
| 52 | + import appService from '../app.service.js' |
| 53 | + export default { |
| 54 | + data () { |
| 55 | + return { |
| 56 | + username: '', |
| 57 | + password: '', |
| 58 | + isAuthenticated: false, |
| 59 | + profile: {} |
| 60 | + } |
| 61 | + }, |
| 62 | + watch: { |
| 63 | + isAuthenticated: function (val) { |
| 64 | + if (val) { |
| 65 | + appService.getProfile() |
| 66 | + .then(profile => { |
| 67 | + this.profile = profile |
| 68 | + }) |
| 69 | + } else { |
| 70 | + this.profile = {} |
| 71 | + } |
| 72 | + } |
| 73 | + }, |
| 74 | + methods: { |
| 75 | + login () { |
| 76 | + appService.login({username: this.username, password: this.password}) |
| 77 | + .then((data) => { |
| 78 | + window.localStorage.setItem('token', data.token) |
| 79 | + window.localStorage.setItem('tokenExpiration', data.expiration) |
| 80 | + this.isAuthenticated = true |
| 81 | + // make sure login form will be empty if user wants to log in again |
| 82 | + this.username = '' |
| 83 | + this.password = '' |
| 84 | + }) |
| 85 | + .catch(() => window.alert('Could not login!')) |
| 86 | + }, |
| 87 | + logout () { |
| 88 | + window.localStorage.setItem('token', null) |
| 89 | + window.localStorage.setItem('tokenExpiration', null) |
| 90 | + this.isAuthenticated = false |
| 91 | + } |
| 92 | + }, |
| 93 | + created () { |
| 94 | + let expiration = window.localStorage.getItem('tokenExpiration') |
| 95 | + let unixTimestamp = new Date().getTime() / 1000 |
| 96 | + if (expiration !== null && parseInt(expiration) - unixTimestamp > 0) { |
| 97 | + this.isAuthenticated = true |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +</script> |
0 commit comments