2017-01-15 19:07:10 +01:00
|
|
|
<template>
|
|
|
|
<md-toolbar id="toolbar" class="md-dense">
|
2017-01-17 19:10:30 +01:00
|
|
|
<span v-if="currentPath != 'home' && currentPath != 'day' && currentPath != 'days'">
|
2017-01-15 22:35:54 +01:00
|
|
|
<md-button class="md-icon-button" @click="goBack()">
|
2017-01-17 19:10:30 +01:00
|
|
|
<md-tooltip md-direction="right">back</md-tooltip>
|
2017-01-15 22:35:54 +01:00
|
|
|
<md-icon>backspace</md-icon>
|
|
|
|
</md-button>
|
|
|
|
</span>
|
|
|
|
<span v-else class="button-spacer"> </span>
|
2017-01-17 19:10:30 +01:00
|
|
|
<h2 id="toolbar-title" class="md-title">
|
|
|
|
<router-link :to="{name: 'home'}" class="home-link">ibt2</router-link>
|
|
|
|
</h2>
|
2017-01-15 19:07:10 +01:00
|
|
|
<span v-if="loggedInUser.username">
|
2017-01-17 18:46:08 +01:00
|
|
|
<md-button v-if="loggedInUser.isAdmin" id="users-icon" class="md-icon-button" @click="toUsersPage()">
|
2017-01-17 19:10:30 +01:00
|
|
|
<md-tooltip md-direction="left">list of users</md-tooltip>
|
2017-01-16 21:05:48 +01:00
|
|
|
<md-icon>people_outline</md-icon>
|
|
|
|
</md-button>
|
2017-01-17 18:46:08 +01:00
|
|
|
<md-button id="logged-in-icon" class="md-icon-button" @click="toUserPage()">
|
2017-01-17 19:10:30 +01:00
|
|
|
<md-tooltip md-direction="left">personal page</md-tooltip>
|
2017-01-15 22:35:54 +01:00
|
|
|
<md-icon>person_pin</md-icon>
|
|
|
|
</md-button>
|
|
|
|
<span id="logged-in" class="md-subheading">
|
|
|
|
<router-link :to="userUrl" class="username-link">{{ loggedInUser.username }}</router-link>
|
2017-01-15 19:07:10 +01:00
|
|
|
</span>
|
2017-01-15 22:35:54 +01:00
|
|
|
<md-button id="logout-icon" class="md-icon-button" @click="logout()">
|
2017-01-17 19:10:30 +01:00
|
|
|
<md-tooltip md-direction="left">logout</md-tooltip>
|
2017-01-15 19:07:10 +01:00
|
|
|
<md-icon>exit_to_app</md-icon>
|
|
|
|
</md-button>
|
|
|
|
</span>
|
|
|
|
<span v-else>
|
2017-01-19 20:46:17 +01:00
|
|
|
<span id="login-form">
|
2017-01-15 19:07:10 +01:00
|
|
|
<md-input-container id="username-input" class="login-input" md-inline>
|
2017-01-17 19:10:30 +01:00
|
|
|
<md-tooltip md-direction="bottom">login name or create a new user if it doesn't exist</md-tooltip>
|
2017-01-15 22:35:54 +01:00
|
|
|
<md-input ref="usernameInput" @keyup.enter.native="focusToPassword()" v-model="username" placeholder="username" md-inline />
|
|
|
|
</md-input-container>
|
|
|
|
<span id="password-block">
|
2017-01-16 21:05:48 +01:00
|
|
|
<md-input-container id="password-input" class="login-input" md-has-password md-inline>
|
2017-01-17 19:10:30 +01:00
|
|
|
<md-tooltip md-direction="bottom">login password or create a new user if it doesn't exist</md-tooltip>
|
2017-01-15 22:35:54 +01:00
|
|
|
<md-input ref="passwordInput" @keyup.enter.native="login()" v-model="password" placeholder="password" type="password" md-line />
|
|
|
|
</md-input-container>
|
|
|
|
<md-button id="login-button" class="md-icon-button" @click="login()">
|
2017-01-17 19:10:30 +01:00
|
|
|
<md-tooltip md-direction="left">login or create a new user if it doesn't exist</md-tooltip>
|
2017-01-15 22:35:54 +01:00
|
|
|
<md-icon>play_circle_outline</md-icon>
|
|
|
|
</md-button>
|
|
|
|
</span>
|
2017-01-15 19:07:10 +01:00
|
|
|
</span>
|
|
|
|
</span>
|
2017-01-19 21:14:52 +01:00
|
|
|
<ibt-snackbar ref="snackbarObj" />
|
2017-01-19 20:46:17 +01:00
|
|
|
<ibt-dialog ref="dialogObj" />
|
2017-01-15 19:07:10 +01:00
|
|
|
</md-toolbar>
|
|
|
|
</template>
|
|
|
|
<script>
|
|
|
|
|
2017-01-19 20:46:17 +01:00
|
|
|
import IbtDialog from './IbtDialog.vue';
|
2017-01-19 21:14:52 +01:00
|
|
|
import IbtSnackbar from './IbtSnackbar.vue';
|
2017-01-19 20:46:17 +01:00
|
|
|
|
2017-01-15 19:07:10 +01:00
|
|
|
export default {
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
username: '',
|
|
|
|
password: '',
|
2017-01-19 20:46:17 +01:00
|
|
|
dialog: {
|
|
|
|
text: 'some error',
|
|
|
|
ok: 'ok'
|
|
|
|
}
|
2017-01-15 19:07:10 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
|
|
|
userUrl: function() {
|
|
|
|
var id = this.loggedInUser._id;
|
|
|
|
if (!id) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return '/user/' + this.loggedInUser._id;
|
2017-01-15 22:35:54 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
loggedInUser() {
|
|
|
|
return this.$store.state.loggedInUser;
|
|
|
|
},
|
|
|
|
|
|
|
|
currentPath() {
|
|
|
|
return this.$route.name;
|
2017-01-15 19:07:10 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeCreate: function() {
|
2017-01-15 22:35:54 +01:00
|
|
|
this.usersUrl = this.$resource('users');
|
2017-01-15 19:07:10 +01:00
|
|
|
this.currentUserUrl = this.$resource('users/current');
|
|
|
|
this.loginUrl = this.$resource('login');
|
|
|
|
this.logoutUrl = this.$resource('logout');
|
|
|
|
},
|
|
|
|
|
|
|
|
mounted: function() {
|
|
|
|
this.getUserInfo();
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
2017-01-15 22:35:54 +01:00
|
|
|
goBack() {
|
|
|
|
this.$router.back();
|
|
|
|
},
|
|
|
|
|
|
|
|
toUserPage() {
|
|
|
|
this.$router.push(this.userUrl);
|
|
|
|
},
|
|
|
|
|
2017-01-16 21:05:48 +01:00
|
|
|
toUsersPage() {
|
|
|
|
this.$router.push('/user/');
|
|
|
|
},
|
|
|
|
|
2017-01-15 19:07:10 +01:00
|
|
|
focusToLoginForm() {
|
|
|
|
var that = this;
|
|
|
|
setTimeout(function() { that.$refs.usernameInput.$el.focus(); }, 400);
|
|
|
|
},
|
|
|
|
|
|
|
|
focusToPassword() {
|
|
|
|
this.$refs.passwordInput.$el.focus();
|
|
|
|
},
|
|
|
|
|
2017-01-16 23:35:28 +01:00
|
|
|
login(opt) {
|
|
|
|
opt = opt || {};
|
2017-01-15 22:35:54 +01:00
|
|
|
var user_data = {username: this.username, password: this.password};
|
|
|
|
this.loginUrl.save(user_data).then((response) => {
|
2017-01-15 19:07:10 +01:00
|
|
|
return response.json();
|
|
|
|
}, (response) => {
|
2017-01-16 23:35:28 +01:00
|
|
|
// Unable to login? Let's try to create the user!
|
2017-01-15 22:35:54 +01:00
|
|
|
if (response.status == 401) {
|
2017-01-16 23:35:28 +01:00
|
|
|
if (opt.stopHere) {
|
2017-01-19 20:46:17 +01:00
|
|
|
this.$refs.dialogObj.show({text: 'failed to login and create a new user. Wrong username and password?'});
|
2017-01-16 23:35:28 +01:00
|
|
|
} else {
|
|
|
|
this.createUser(user_data);
|
|
|
|
}
|
2017-01-15 22:35:54 +01:00
|
|
|
}
|
2017-01-15 19:07:10 +01:00
|
|
|
}).then((data) => {
|
|
|
|
this.getUserInfo();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
logout() {
|
|
|
|
this.logoutUrl.get().then((response) => {
|
|
|
|
return response.json();
|
|
|
|
}, (response) => {
|
2017-01-19 20:46:17 +01:00
|
|
|
this.$refs.dialogObj.show({text: 'failed to logout'});
|
2017-01-15 19:07:10 +01:00
|
|
|
}).then((json) => {
|
2017-01-15 22:35:54 +01:00
|
|
|
this.$store.commit('clearLoggedInUser');
|
2017-01-19 21:14:52 +01:00
|
|
|
this.$refs.snackbarObj.show('logged out');
|
2017-01-15 22:35:54 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
createUser(user_data) {
|
|
|
|
user_data.username = user_data.username || {};
|
|
|
|
user_data.username = user_data.username || this.username;
|
|
|
|
user_data.password = user_data.password || this.password;
|
|
|
|
this.usersUrl.save(user_data).then((response) => {
|
|
|
|
return response.json();
|
|
|
|
}, (response) => {
|
|
|
|
}).then((json) => {
|
2017-01-16 23:35:28 +01:00
|
|
|
this.login({stopHere: true});
|
2017-01-15 19:07:10 +01:00
|
|
|
});
|
2017-01-15 22:35:54 +01:00
|
|
|
|
2017-01-15 19:07:10 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getUserInfo(callback) {
|
|
|
|
this.currentUserUrl.get().then((response) => {
|
|
|
|
return response.json();
|
|
|
|
}, (response) => {
|
2017-01-19 20:46:17 +01:00
|
|
|
this.$refs.dialogObj.show({text: 'unable to get user info'});
|
2017-01-15 19:07:10 +01:00
|
|
|
}).then((data) => {
|
2017-01-15 22:35:54 +01:00
|
|
|
data = data || {};
|
|
|
|
this.$store.commit('setLoggedInUser', data);
|
2017-01-15 19:07:10 +01:00
|
|
|
if (callback) {
|
2017-01-15 22:35:54 +01:00
|
|
|
callback(data);
|
2017-01-15 19:07:10 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-01-19 20:46:17 +01:00
|
|
|
},
|
|
|
|
|
2017-01-19 21:14:52 +01:00
|
|
|
components: { IbtDialog, IbtSnackbar }
|
2017-01-15 19:07:10 +01:00
|
|
|
}
|
2017-01-19 22:21:50 +01:00
|
|
|
|
2017-01-15 19:07:10 +01:00
|
|
|
</script>
|
|
|
|
<style>
|
2017-01-19 22:21:50 +01:00
|
|
|
|
2017-01-15 19:07:10 +01:00
|
|
|
#toolbar-title {
|
|
|
|
flex: 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
.login-input {
|
|
|
|
width: 200px;
|
|
|
|
margin: 0px 0px 0px;
|
|
|
|
padding-top: 0px;
|
|
|
|
padding-left: 4px;
|
|
|
|
min-height: 24px;
|
|
|
|
line-height: 0px;
|
|
|
|
background-color: white;
|
|
|
|
}
|
|
|
|
|
|
|
|
#username-input {
|
|
|
|
display: inline;
|
|
|
|
float: left;
|
2017-01-15 22:35:54 +01:00
|
|
|
margin-right: 20px;
|
2017-01-15 19:07:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#password-input {
|
2017-01-15 22:35:54 +01:00
|
|
|
display: inline;
|
|
|
|
float: left;
|
|
|
|
}
|
|
|
|
|
|
|
|
#password-block {
|
2017-01-15 19:07:10 +01:00
|
|
|
display: inline;
|
|
|
|
float: right;
|
|
|
|
}
|
|
|
|
|
2017-01-15 22:35:54 +01:00
|
|
|
#login-button {
|
|
|
|
height: 32px;
|
|
|
|
}
|
|
|
|
|
|
|
|
#logged-in-icon {
|
|
|
|
margin-right: 0px;
|
|
|
|
padding-right: 0px;
|
|
|
|
color: #f6f72f !important;
|
|
|
|
}
|
|
|
|
|
2017-01-15 19:07:10 +01:00
|
|
|
#logged-in {
|
|
|
|
position: relative;
|
|
|
|
top: 10px;
|
|
|
|
|
|
|
|
}
|
2017-01-15 22:35:54 +01:00
|
|
|
|
|
|
|
#logout-icon {
|
|
|
|
margin-left: 0px;
|
|
|
|
padding-left: 0px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.username-link {
|
|
|
|
font-weight: bold;
|
|
|
|
color: #f6f72f !important;
|
|
|
|
}
|
|
|
|
|
|
|
|
.button-spacer {
|
|
|
|
width: 52px;
|
|
|
|
}
|
|
|
|
|
2017-01-16 21:05:48 +01:00
|
|
|
.home-link {
|
|
|
|
font-weight: bold;
|
|
|
|
color: white !important;
|
|
|
|
}
|
2017-01-19 22:21:50 +01:00
|
|
|
|
2017-01-15 19:07:10 +01:00
|
|
|
</style>
|