-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from stanislawK/35_login_component
[35] login component
- Loading branch information
Showing
13 changed files
with
284 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ psycopg2==2.7.6 | |
pytest==4.1.0 | ||
marshmallow==3.0.0rc1 | ||
flask-jwt-extended==3.14.0 | ||
flask-cors==3.0.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,11 @@ | ||
<template> | ||
<div id="app"> | ||
<img alt="Vue logo" src="./assets/logo.png"> | ||
<HelloWorld msg="Welcome to Your Vue.js App"/> | ||
<div> | ||
<router-view></router-view> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import HelloWorld from './components/HelloWorld.vue' | ||
export default { | ||
name: 'app', | ||
components: { | ||
HelloWorld | ||
} | ||
name: 'app' | ||
} | ||
</script> | ||
|
||
<style> | ||
#app { | ||
font-family: 'Avenir', Helvetica, Arial, sans-serif; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
text-align: center; | ||
color: #2c3e50; | ||
margin-top: 60px; | ||
} | ||
</style> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<template> | ||
<div> | ||
<v-app> | ||
</v-app> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
} | ||
</script> | ||
|
||
<style lang="css"> | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<template> | ||
<div class="register-form"> | ||
<v-app id="login"> | ||
<v-layout justify-center> | ||
<v-flex xs12 sm6> | ||
<v-alert | ||
type="error" | ||
:value="error_msg ? true : false" | ||
@click="error_msg=''" | ||
transition="slide-y-transition" | ||
>{{error_msg}}</v-alert> | ||
<v-alert | ||
type="success" | ||
:value="successAlert" | ||
@click="successAlert = !successAlert" | ||
transition="slide-y-transition" | ||
>Pomyślnie zalogowano</v-alert> | ||
<v-progress-circular | ||
v-if="spinner" | ||
:size="50" | ||
color="green" | ||
indeterminate> | ||
</v-progress-circular> | ||
<form> | ||
<v-text-field | ||
v-model="username" | ||
label="Nazwa użytkownika" | ||
required | ||
:error-messages="usernameErrors" | ||
@input="$v.username.$touch()" | ||
@blur="$v.username.$touch()" | ||
></v-text-field> | ||
<v-text-field | ||
v-model="password" | ||
label="Hasło" | ||
:append-icon="showPass ? 'visibility_off' : 'visibility'" | ||
:type="showPass ? 'text' : 'password'" | ||
@click:append="showPass = !showPass" | ||
required | ||
:error-messages="passwordErrors" | ||
@input="$v.password.$touch()" | ||
@blur="$v.password.$touch()" | ||
></v-text-field> | ||
<v-btn @click="onSubmit">Zaloguj</v-btn> | ||
</form> | ||
</v-flex> | ||
</v-layout> | ||
</v-app> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { minLength, required } from 'vuelidate/lib/validators' | ||
export default { | ||
data() { | ||
return { | ||
username: '', | ||
password: '', | ||
showPass: false, | ||
error_msg: '', | ||
successAlert: false, | ||
showSpinner: false | ||
} | ||
}, | ||
validations: { | ||
username: { required, minLength: minLength(5) }, | ||
password: { required } | ||
}, | ||
methods: { | ||
onSubmit() { | ||
const loginData = { | ||
username: this.username, | ||
password: this.password | ||
} | ||
this.error_msg = '' | ||
if(!this.$v.$invalid) { | ||
this.spinner = true | ||
this.$store.dispatch('auth/login', loginData) | ||
.then( res => { | ||
this.spinner = false | ||
this.clearForm() | ||
if (res.status == 201) this.successAlert = true; | ||
}, error => { | ||
this.spinner = false | ||
this.clearForm() | ||
const err = error.response.data.msg | ||
if (err == "Not authorized") { | ||
this.error_msg = "Nieprawidłowa nazwa użytkownika, lub hasło" | ||
} else if (err.includes('User already logged in')) { | ||
this.error_msg = "Jesteś już zalogowany" | ||
} else if (error) { | ||
this.error_msg = "Logowanie nie powiodło się" | ||
} | ||
}) | ||
} | ||
}, | ||
clearForm() { | ||
this.$v.$reset() | ||
this.username = '' | ||
this.password = '' | ||
} | ||
}, | ||
computed: { | ||
usernameErrors() { | ||
const errors = [] | ||
if (!this.$v.username.$dirty) return errors | ||
!this.$v.username.minLength && errors.push( | ||
'Wprowadź poprawną nazwę użytkownika' | ||
) | ||
!this.$v.username.required && errors.push( | ||
'Nazwa użytkownika jest wymagana' | ||
) | ||
return errors | ||
}, | ||
passwordErrors() { | ||
const errors = [] | ||
if (!this.$v.password.$dirty) return errors | ||
!this.$v.password.required && errors.push( | ||
'Hasło jest wymagane' | ||
) | ||
return errors | ||
}, | ||
}, | ||
} | ||
</script> | ||
|
||
<style> | ||
.v-messages__message { | ||
padding: 2px; | ||
} | ||
#login { | ||
margin-top: 20vh; | ||
margin-left: 2em; | ||
margin-right: 2em; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
import Vue from 'vue' | ||
import App from './App.vue' | ||
import Vuelidate from 'vuelidate' | ||
import Vuetify from 'vuetify' | ||
import 'vuetify/dist/vuetify.min.css' | ||
|
||
import router from './router' | ||
import store from './store' | ||
|
||
Vue.use(Vuelidate) | ||
Vue.use(Vuetify) | ||
|
||
Vue.config.productionTip = false | ||
|
||
new Vue({ | ||
router, | ||
store, | ||
render: h => h(App), | ||
}).$mount('#app') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Vue from 'vue' | ||
import VueRouter from 'vue-router' | ||
|
||
import HomePage from './components/HomePage.vue' | ||
import Login from './components/Login.vue' | ||
|
||
Vue.use(VueRouter) | ||
|
||
const routes = [ | ||
{ path: '/', component: HomePage }, | ||
{ path: '/login', component: Login } | ||
] | ||
|
||
export default new VueRouter({mode: 'history', routes}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Vue from 'vue'; | ||
import Vuex from 'vuex'; | ||
import auth from './modules/auth' | ||
|
||
Vue.use(Vuex); | ||
|
||
const store = new Vuex.Store({ | ||
state: {}, | ||
getters: {}, | ||
mutations: {}, | ||
actions : {}, | ||
|
||
modules: { | ||
auth | ||
} | ||
}) | ||
|
||
export default store; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import axios from 'axios'; | ||
axios.defaults.baseURL = 'http://0.0.0.0:5000/' | ||
|
||
export default { | ||
namespaced: true, | ||
state: { | ||
token: localStorage.getItem('token') || '', | ||
}, | ||
getters: { | ||
isLoggedIn: state => !!state.token, | ||
}, | ||
mutations: { | ||
auth_success(state, token) { | ||
state.token = token | ||
} | ||
}, | ||
actions: { | ||
login({commit, dispatch}, loginData) { | ||
return new Promise ((resolve, reject) => { | ||
axios.post('auth/login', { | ||
username: loginData.username, | ||
password: loginData.password | ||
}) | ||
.then( res => { | ||
const token = res.data.access_token | ||
localStorage.setItem('token', token) | ||
axios.defaults.headers.common['Authorization'] = 'Bearer ' + token | ||
commit('auth_success', token) | ||
resolve(res) | ||
}, error => { | ||
localStorage.removeItem('token') | ||
reject(error) | ||
}) | ||
}) | ||
} | ||
} | ||
} |
Oops, something went wrong.