Merge pull request #335 from vatesfr/passport
Authentication is moved to xo-server.
This commit is contained in:
24
app/app.js
24
app/app.js
@@ -20,7 +20,6 @@ import deleteVmsState from './modules/delete-vms'
|
||||
import genericModalState from './modules/generic-modal'
|
||||
import hostState from './modules/host'
|
||||
import listState from './modules/list'
|
||||
import loginState from './modules/login'
|
||||
import navbarState from './modules/navbar'
|
||||
import newSrState from './modules/new-sr'
|
||||
import newVmState from './modules/new-vm'
|
||||
@@ -57,7 +56,6 @@ export default angular.module('xoWebApp', [
|
||||
genericModalState,
|
||||
hostState,
|
||||
listState,
|
||||
loginState,
|
||||
navbarState,
|
||||
newSrState,
|
||||
newVmState,
|
||||
@@ -125,26 +123,12 @@ export default angular.module('xoWebApp', [
|
||||
xoApi
|
||||
) {
|
||||
$rootScope.$on('$stateChangeStart', function (event, state, stateParams) {
|
||||
let {user} = xoApi
|
||||
let loggedIn = !!user
|
||||
|
||||
if (state.name === 'login') {
|
||||
if (loggedIn) {
|
||||
event.preventDefault()
|
||||
$state.go('index')
|
||||
}
|
||||
|
||||
const {user} = xoApi
|
||||
if (!user) {
|
||||
return
|
||||
}
|
||||
// TODO: we should redirect to a login page.
|
||||
|
||||
if (!loggedIn) {
|
||||
event.preventDefault()
|
||||
|
||||
// FIXME: find a better way to pass info to the login controller.
|
||||
$rootScope._login = { state, stateParams }
|
||||
|
||||
$state.go('login')
|
||||
return
|
||||
throw new Error('the user should be logged in')
|
||||
}
|
||||
|
||||
if (user.permission === 'admin') {
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
import angular from 'angular'
|
||||
import uiRouter from 'angular-ui-router'
|
||||
|
||||
import view from './view'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
export default angular.module('xoWebApp.login', [
|
||||
uiRouter
|
||||
])
|
||||
.config(function ($stateProvider) {
|
||||
$stateProvider.state('login', {
|
||||
url: '/login',
|
||||
controller: 'LoginCtrl',
|
||||
template: view
|
||||
})
|
||||
})
|
||||
.controller('LoginCtrl', function ($scope, $state, $rootScope, xoApi, notify) {
|
||||
const {toState, toStateParams} = (function (login) {
|
||||
if (login) {
|
||||
delete $rootScope._login
|
||||
|
||||
return {
|
||||
toState: login.state.name,
|
||||
toStateParams: login.stateParams
|
||||
}
|
||||
}
|
||||
|
||||
return { toState: 'index' }
|
||||
})($rootScope._login)
|
||||
|
||||
$scope.$watch(() => xoApi.user, function (user) {
|
||||
// When the user is logged in, go the wanted view, fallbacks on
|
||||
// the index view if necessary.
|
||||
if (user) {
|
||||
$state.go(toState, toStateParams).catch(function () {
|
||||
$state.go('index')
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
Object.defineProperties($scope, {
|
||||
user: {
|
||||
get () {
|
||||
return xoApi.user
|
||||
}
|
||||
},
|
||||
status: {
|
||||
get () {
|
||||
return xoApi.status
|
||||
}
|
||||
}
|
||||
})
|
||||
$scope.logIn = (...args) => {
|
||||
xoApi.logIn(...args).catch(error => {
|
||||
notify.warning({
|
||||
title: 'Authentication failed',
|
||||
message: error.message
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// A module exports its name.
|
||||
.name
|
||||
@@ -1,54 +0,0 @@
|
||||
//- Hide the navbar for this view.
|
||||
style.
|
||||
.navbar {
|
||||
display: none;
|
||||
}
|
||||
|
||||
div.container
|
||||
div.row-login
|
||||
div.page-header
|
||||
img(src = 'images/logo_small.png')
|
||||
h2 Xen Orchestra
|
||||
form.form-horizontal(
|
||||
ng-submit = '$broadcast("fixAutofill"); logIn(email, password, true)'
|
||||
)
|
||||
fieldset
|
||||
legend.login: h3 Sign in
|
||||
div.form-group
|
||||
div.col-sm-12
|
||||
.input-group
|
||||
span.input-group-addon: i.xo-icon-user.fa-fw
|
||||
input.form-control.input-sm(
|
||||
name = 'email'
|
||||
type = 'text'
|
||||
placeholder = 'Username'
|
||||
ng-model = 'email'
|
||||
required
|
||||
fix-autofill
|
||||
)
|
||||
div.form-group
|
||||
div.col-sm-12
|
||||
.input-group
|
||||
span.input-group-addon: i.fa.fa-key.fa-fw
|
||||
input.form-control.input-sm(
|
||||
name = 'password'
|
||||
type = 'password'
|
||||
placeholder = 'Password'
|
||||
ng-model = 'password'
|
||||
required
|
||||
fix-autofill
|
||||
)
|
||||
div.form-group
|
||||
div.col-sm-12
|
||||
button.btn.btn-login.btn-block.btn-success(
|
||||
id = 'login'
|
||||
name = 'login'
|
||||
)
|
||||
i.fa.fa-sign-in
|
||||
| Login
|
||||
p.status(ng-if = '"disconnected" === status')
|
||||
i.xo-icon-error.fa-2x(tooltip = 'You are not connected to XO-Server')
|
||||
p.status(ng-if = '"connecting" === status')
|
||||
i.fa.fa-refresh.fa-spin.fa-2x(tooltip = 'Connecting to XO-Server')
|
||||
p.status(ng-if = '"connected" === status')
|
||||
i.xo-icon-success.fa-2x(tooltip = 'You are connected to XO-Server')
|
||||
@@ -28,7 +28,6 @@ export default angular.module('xoWebApp.navbar', [
|
||||
this.logIn = xoApi.logIn
|
||||
this.logOut = function () {
|
||||
xoApi.logOut()
|
||||
$state.go('login')
|
||||
}
|
||||
|
||||
// When a searched is entered, we must switch to the list view if
|
||||
|
||||
34
app/node_modules/xo-api/index.js
generated
vendored
34
app/node_modules/xo-api/index.js
generated
vendored
@@ -25,9 +25,10 @@ export default angular.module('xo-api', [
|
||||
})
|
||||
})
|
||||
.service('xoApi', function (
|
||||
$cookieStore,
|
||||
$cookies,
|
||||
$rootScope,
|
||||
$timeout
|
||||
$timeout,
|
||||
$window
|
||||
) {
|
||||
const xo = new xoLib.Xo()
|
||||
|
||||
@@ -39,20 +40,7 @@ export default angular.module('xo-api', [
|
||||
$rootScope.$applyAsync()
|
||||
})
|
||||
|
||||
try {
|
||||
const token = $cookieStore.get('token')
|
||||
|
||||
// If there is a token, sign in with it.
|
||||
if (token) {
|
||||
xo.signIn({ token })
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof SyntaxError) {
|
||||
$cookieStore.remove('token')
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
xo.signIn({ token: $cookies.get('token') })
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
|
||||
@@ -335,19 +323,11 @@ export default angular.module('xo-api', [
|
||||
// Session
|
||||
// -----------------
|
||||
|
||||
logIn (email, password, persist) {
|
||||
return xo.signIn({ email, password }).then(() => {
|
||||
if (persist) {
|
||||
xo.call('token.create').then(function (token) {
|
||||
$cookieStore.put('token', token)
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
logOut () {
|
||||
$cookieStore.remove('token')
|
||||
$cookies.remove('token')
|
||||
|
||||
return xo.signOut()
|
||||
// Full page reload.
|
||||
$window.location.reload(true)
|
||||
},
|
||||
get status () {
|
||||
return xo.status
|
||||
|
||||
Reference in New Issue
Block a user