Skip to content

Commit

Permalink
basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
thesubhendu committed Feb 5, 2018
1 parent fa271f5 commit e8fa1c6
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 18 deletions.
43 changes: 40 additions & 3 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@
<div id="app">

<div class="navbar">
<router-link to="/" class="navbar-brand">VueFire</router-link>
<ul class="nav navbar-nav">

<li>
<router-link to="/">VueFire</router-link>
</li>
<li v-if="!authUser">
<router-link to="/sign-in">SignIn</router-link>
</li>

<li>
<li v-if="!authUser">
<router-link to="/sign-up">SignUp</router-link>
</li>

<li v-if="authUser">
<a @click="logout"> Logout</a>
<a href="#">{{authUser.identifier}}</a>

</li>
</ul>


</div>

<img src="./assets/logo.png">
Expand All @@ -22,7 +32,34 @@

<script>
export default {
name: 'App'
name: 'App',
data(){
return {
authUser:null
}
},
watch:{
'$route':'setAuthUser'
},
methods:{
setAuthUser(){
this.authUser=firebase.auth().currentUser;
},
logout(){
firebase.auth().signOut()
.then(()=>{
this.$router.replace('/sign-in')
})
.catch((e)=>{
alert(e.message)
})
}
},
created(){
this.setAuthUser();
// this.authUser=firebase.auth().currentUser;
}
}
</script>

Expand Down
15 changes: 13 additions & 2 deletions src/components/SignIn.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<br>
<input type="password" v-model="formData.password" class="form-control" placeholder="password">
<br>
<button class="btn btn-success">Signin</button>
<button class="btn btn-success" @click="signIn">Signin</button>
</div>

</div>
Expand All @@ -26,7 +26,18 @@
}
}
},
methods: {},
methods: {
signIn(){
firebase.auth().signInWithEmailAndPassword(this.formData.email,this.formData.password)
.then((user)=>{
this.$router.replace('/hello')
})
.catch((e)=>{
alert(e.message)
})
}
},
created(){
Expand Down
4 changes: 1 addition & 3 deletions src/components/Signup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
<br>
<input type="password" v-model="formData.password" class="form-control" placeholder="password">
<br>
<input type="password" class="form-control" placeholder="confirm password">
<br>
<button class="btn btn-success" @click="signUp">SignUp</button>
</div>

Expand All @@ -31,7 +29,7 @@
signUp(){
firebase.auth().createUserWithEmailAndPassword(this.formData.email,this.formData.password)
.then((user)=>{
alert('created')
this.$router.replace('/hello')
})
.catch((e)=>{
alert('oops'+e.message);
Expand Down
19 changes: 12 additions & 7 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import firebase from 'firebase'

Vue.config.productionTip = false


let app;
var config = {
apiKey: "AIzaSyDlFQmbshbz32CxUn8mFHfGm7opw5UhUZg",
authDomain: "tinkering-6f440.firebaseapp.com",
Expand All @@ -22,9 +22,14 @@ firebase.initializeApp(config);
window.firebase=firebase;

/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
firebase.auth().onAuthStateChanged((user)=>{
if(!app){
app= new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
});
}
});

27 changes: 24 additions & 3 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,43 @@ import Signup from '@/components/Signup'

Vue.use(Router)

export default new Router({
let router = new Router({
routes: [
{
path: '/hello',
name: 'HelloWorld',
component: HelloWorld
component: HelloWorld,
meta:{
requiresAuth:true
}
},
{
path: '/sign-in',
name: 'SignIn',
component: SignIn
},
{
path: '*',
redirect: '/sign-in',
},
{
path: '/sign-up',
name: 'Signup',
component: Signup
}
]
})
});

router.beforeEach((to,from,next)=>{
let currentUser=firebase.auth().currentUser;
let requiresAuth=to.matched.some(record=>record.meta.requiresAuth);

if(requiresAuth && !currentUser) next('sign-in')
else if (!requiresAuth && currentUser) next('hello')
else next()
});

export default router



0 comments on commit e8fa1c6

Please sign in to comment.