Skip to content

Commit a6fdc4b

Browse files
navbar state and user login api
Signed-off-by: Arnav Gupta <[email protected]>
1 parent 706e686 commit a6fdc4b

File tree

9 files changed

+136
-18
lines changed

9 files changed

+136
-18
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"test:unit": "vue-cli-service test:unit"
1111
},
1212
"dependencies": {
13+
"axios": "^0.18.0",
1314
"vue": "^2.5.17",
1415
"vue-class-component": "^6.0.0",
1516
"vue-property-decorator": "^7.2.0",

src/components/AppNavbar.vue

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,39 @@
77
<!-- Add "active" class when you're on that page" -->
88
<a class="nav-link active" href="">Home</a>
99
</li>
10-
<li class="nav-item">
10+
<li v-if="username" class="nav-item">
1111
<a class="nav-link" href="">
1212
<i class="ion-compose"></i>&nbsp;New Post
1313
</a>
1414
</li>
15-
<li class="nav-item">
15+
<li v-if="username" class="nav-item">
1616
<a class="nav-link" href="">
1717
<i class="ion-gear-a"></i>&nbsp;Settings
1818
</a>
1919
</li>
20-
<li class="nav-item"><a class="nav-link" href="">Sign up</a></li>
20+
<li v-if="!username" class="nav-item">
21+
<router-link class="nav-link" to="/register">
22+
Sign up
23+
</router-link>
24+
</li>
25+
<li v-if="!username" class="nav-item">
26+
<router-link class="nav-link" to="/login">
27+
Sign in
28+
</router-link>
29+
</li>
2130
</ul>
2231
</div>
2332
</nav>
2433
</template>
2534

2635
<script>
2736
import { Vue, Component } from 'vue-property-decorator';
37+
import users from '@/store/modules/users'
2838
2939
@Component
30-
export default class AppNavbar extends Vue {}
40+
export default class AppNavbar extends Vue {
41+
get username() {
42+
return users.username
43+
}
44+
}
3145
</script>

src/store/api.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import axios from 'axios';
2+
import { UserSubmit, UserResponse, User } from './models';
3+
4+
export const conduitApi = axios.create({
5+
baseURL: 'https://conduit.productionready.io/api',
6+
});
7+
8+
export function setJWT(jwt: string) {
9+
conduitApi.defaults.headers.common['Authorization'] = `Token ${jwt}`;
10+
}
11+
12+
export function clearJWT() {
13+
delete conduitApi.defaults.headers.common['Authorization'];
14+
}
15+
16+
export async function loginUser(user: UserSubmit): Promise<User> {
17+
const response = await conduitApi.post('/users/login', {
18+
user,
19+
});
20+
return (response.data as UserResponse).user;
21+
}

src/store/models.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
export interface Profile {
3+
username: string;
4+
bio?: string;
5+
image?: string;
6+
following: boolean;
7+
}
8+
9+
export interface User {
10+
email: string;
11+
token: string;
12+
username: string;
13+
bio?: string;
14+
image?: string;
15+
}
16+
17+
export interface UserSubmit {
18+
email: string;
19+
password: string;
20+
}
21+
22+
export interface UserResponse {
23+
user: User
24+
}

src/store/modules/articles.ts

Whitespace-only changes.

src/store/modules/users.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { VuexModule, Module, getModule, MutationAction, Mutation, Action } from 'vuex-module-decorators'
2+
import store from '@/store'
3+
import { User, Profile, UserSubmit } from '../models';
4+
import { loginUser } from '../api';
5+
6+
@Module({
7+
namespaced: true,
8+
name: 'users',
9+
store,
10+
dynamic: true
11+
})
12+
class UsersModule extends VuexModule {
13+
user: User | null = null
14+
profile: Profile | null = null
15+
16+
@Mutation
17+
setUser(user: User) { this.user = user }
18+
19+
get username() {
20+
return this.user && this.user.username || null
21+
}
22+
23+
@Action({commit: 'setUser'})
24+
async login(userSubmit: UserSubmit) {
25+
const user = await loginUser(userSubmit)
26+
return user
27+
}
28+
}
29+
30+
export default getModule(UsersModule);

src/views/Login.vue

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,58 @@
33
<div class="container page">
44
<div class="row">
55
<div class="col-md-6 offset-md-3 col-xs-12">
6-
<h1 class="text-xs-center">Sign up</h1>
7-
<p class="text-xs-center"><a href="">Have an account?</a></p>
6+
<h1 class="text-xs-center">Sign in</h1>
7+
<p class="text-xs-center">
8+
<router-link to="/register">
9+
Need an account?
10+
</router-link>
11+
</p>
812

9-
<ul class="error-messages">
13+
<!-- <ul class="error-messages">
1014
<li>That email is already taken</li>
11-
</ul>
15+
</ul> -->
1216

1317
<form>
1418
<fieldset class="form-group">
1519
<input
1620
class="form-control form-control-lg"
1721
type="text"
18-
placeholder="Your Name"
19-
/>
20-
</fieldset>
21-
<fieldset class="form-group">
22-
<input
23-
class="form-control form-control-lg"
24-
type="text"
22+
v-model="email"
2523
placeholder="Email"
2624
/>
2725
</fieldset>
2826
<fieldset class="form-group">
2927
<input
3028
class="form-control form-control-lg"
3129
type="password"
30+
v-model="password"
3231
placeholder="Password"
3332
/>
3433
</fieldset>
35-
<button class="btn btn-lg btn-primary pull-xs-right">
36-
Sign up
34+
<button @click="login()" class="btn btn-lg btn-primary pull-xs-right">
35+
Sign in
3736
</button>
3837
</form>
3938
</div>
4039
</div>
4140
</div>
4241
</div>
4342
</template>
43+
44+
<script>
45+
import { Vue, Component } from 'vue-property-decorator';
46+
import users from '@/store/modules/users'
47+
48+
@Component
49+
export default class Login extends Vue {
50+
email = ''
51+
password = ''
52+
53+
login() {
54+
users.login({
55+
email: this.email,
56+
password: this.password
57+
})
58+
}
59+
}
60+
</script>

vue.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
transpileDependencies: ['vuex-module-decorators'],
3+
};

yarn.lock

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,14 @@ aws4@^1.6.0, aws4@^1.8.0:
15271527
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.8.0.tgz#f0e003d9ca9e7f59c7a508945d7b2ef9a04a542f"
15281528
integrity sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==
15291529

1530+
axios@^0.18.0:
1531+
version "0.18.0"
1532+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.18.0.tgz#32d53e4851efdc0a11993b6cd000789d70c05102"
1533+
integrity sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=
1534+
dependencies:
1535+
follow-redirects "^1.3.0"
1536+
is-buffer "^1.1.5"
1537+
15301538
babel-code-frame@^6.22.0, babel-code-frame@^6.26.0:
15311539
version "6.26.0"
15321540
resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b"
@@ -4199,7 +4207,7 @@ flush-write-stream@^1.0.0:
41994207
inherits "^2.0.1"
42004208
readable-stream "^2.0.4"
42014209

4202-
follow-redirects@^1.0.0:
4210+
follow-redirects@^1.0.0, follow-redirects@^1.3.0:
42034211
version "1.5.10"
42044212
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a"
42054213
integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==

0 commit comments

Comments
 (0)