Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit 8df0ef0

Browse files
author
Dean Sofer
committed
Added Auth service
1 parent 6bbc87b commit 8df0ef0

File tree

10 files changed

+49
-28
lines changed

10 files changed

+49
-28
lines changed

modules/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var module = angular.module('App', ['ui.router', 'App.Authentication', 'App.Guest']);
1+
var module = angular.module('App', ['ui.router', 'App.Auth', 'App.Guest']);
22

33
module.config(function($urlRouterProvider) {
44

modules/App.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Authentication/Authenticated.html
1+
// Auth/Authenticated.html
22
.breadcrumbs {
33
> span {
44
&:after {

modules/Auth/Auth.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var module = angular.module('App.Auth', ['ui.router', 'ui.bootstrap']);
2+
3+
module.factory('Auth', ($q, $http) => {
4+
/**
5+
* Auth
6+
*/
7+
return {
8+
/**
9+
* Retrieves the currently logged in user
10+
* @returns {Promise<User>}
11+
*/
12+
checkCredentials() {
13+
return $http.get('/api/session');
14+
},
15+
/**
16+
* @param {UserObject} user
17+
* @param {boolean} rememberMe
18+
*/
19+
login(user, rememberMe = false) {
20+
return $http.post('/api/session', user);
21+
}
22+
}
23+
});
File renamed without changes.

modules/Authentication/Authenticated.js renamed to modules/Auth/Authenticated.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
/*
2-
Authentication Module
2+
Auth Module
33
=====================
44
55
Most of the actual app is located under here. This abstract module primarily tackles authenticating the user
66
*/
77

8-
var module = angular.module('App.Authenticated', ['ui.router']);
8+
var module = angular.module('App.Auth');
99

1010
module.config(function($stateProvider) {
1111
$stateProvider.state('authenticated', {
1212
templateUrl: 'modules/Authenticated/Authenticated.html',
1313
abstract: true,
1414
resolve: {
15-
user: (User, Authentication, $state, $q) => {
16-
return Authentication.checkCredentials()
15+
user: (User, Auth, $state, $q) =>
16+
Auth.checkCredentials()
1717
.then(
1818
(response) => new User(response.data),
1919
// must return a rejected promise in order to stay in rejected-mode
2020
(error) => $q.reject($state.go('login'))
21-
);
21+
)
2222
},
2323
// layout variable for breadcrumb nav (populated by children)
2424
breadcrumbs: () => []
File renamed without changes.

modules/Authentication/Login.js renamed to modules/Auth/Login.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ $modal is part of ui.bootstrap
99
// Tracks the previous location and allows you to redirect back to that location
1010
var previousLocation;
1111

12-
var module = angular.module('App.Login', ['ui.router', 'ui.bootstrap']);
12+
var module = angular.module('App.Auth');
1313

1414
module.config(function($stateProvider) {
1515
$stateProvider.state('login', {
@@ -25,7 +25,7 @@ module.config(function($stateProvider) {
2525
};
2626
},
2727
modal: ($modal) => $modal.open({
28-
templateUrl: 'modules/Authentication/Login.html',
28+
templateUrl: 'modules/Auth/Login.html',
2929
controller: 'Login',
3030
})
3131
},
@@ -42,11 +42,11 @@ module.run(function($rootScope, $location) {
4242
});
4343
});
4444

45-
module.controller('Login', ($scope, UserObject, Authentication, user, redirect) => {
45+
module.controller('Login', ($scope, UserObject, Auth, user, redirect) => {
4646
$scope.user = user;
4747

4848
$scope.login = () => {
49-
Authentication.login($scope.user, $scope.rememberMe)
49+
Auth.login($scope.user, $scope.rememberMe)
5050
.then( redirect, (response) => $scope.error = response);
5151
};
5252
});

modules/Intro/Intro.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ module.config(function($stateProvider) {
2323
url: '/projects', // /intro/projects
2424
views: {
2525
'modal@': { // The $modal service places HTML at the top of the document. I know, it's weird.
26-
templateUrl: 'modules/Intro/projects.html'
26+
templateUrl: 'modules/Intro/Projects.html'
2727
}
2828
},
2929
});
3030
$stateProvider.state( 'intro.tasks', {
3131
url: '/tasks', // /intro/tasks
3232
views: {
3333
'modal@': { // The $modal service places HTML at the top of the document. I know, it's weird.
34-
templateUrl: 'modules/Intro/tasks.html'
34+
templateUrl: 'modules/Intro/Tasks.html'
3535
}
3636
},
3737
});
3838
$stateProvider.state( 'intro.done', {
3939
url: '/done', // /intro/done
4040
views: {
4141
'modal@': { // The $modal service places HTML at the top of the document. I know, it's weird.
42-
templateUrl: 'modules/Intro/done.html'
42+
templateUrl: 'modules/Intro/Done.html'
4343
}
4444
},
4545
});

modules/Task/Task.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ module.config( ($stateProvider) => {
1414
tasks: (Task, project) => Task.list(project.id)
1515
},
1616
// breadcrumbs resolved in authenticated state
17-
onEnter: function(breadcrumbs) {
17+
onEnter(breadcrumbs) {
1818
breadcrumbs.push({ label: 'Tasks', sref: 'tasks' });
1919
},
20-
onExit: function(breadcrumbs) {
20+
onExit(breadcrumbs) {
2121
breadcrumbs.pop();
2222
}
2323
});
@@ -30,10 +30,10 @@ module.config( ($stateProvider) => {
3030
templateUrl: 'modules/Task/Form.html',
3131
controller: 'TaskForm',
3232
// breadcrumbs resolved in authenticated state
33-
onEnter: function(breadcrumbs) {
33+
onEnter(breadcrumbs) {
3434
breadcrumbs.push({ label: 'New', sref: 'tasks.new' });
3535
},
36-
onExit: function(breadcrumbs) {
36+
onExit(breadcrumbs) {
3737
breadcrumbs.pop();
3838
}
3939
});
@@ -54,11 +54,11 @@ module.config( ($stateProvider) => {
5454
resolve: {
5555
task: (project, $stateParams) => project.getTask($stateParams.taskId)
5656
},
57-
onEnter: function(task, breadcrumbs) {
57+
onEnter(task, breadcrumbs) {
5858
task.open();
5959
breadcrumbs.push({ label: task.name, sref: 'task' });
6060
},
61-
onExit: function(task, breadcrumbs) {
61+
onExit(task, breadcrumbs) {
6262
task.close();
6363
breadcrumbs.pop();
6464
}

modules/Task/TaskObject.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,22 @@ module.factory( 'Task', (BaseObject, $http) => {
1414

1515
/**
1616
* Creates a new task
17-
*
17+
*
1818
* @note Demonstrates how to have the create wait until uploading is complete
1919
* Normally you might have to wait until the attachment is done before enabling
2020
* submission of the form, but this way allows the user to submit before uploading
2121
* is complete and makes it easy to show loading spinners.
22-
*
22+
*
2323
* You could check either the `task.saving`, `task.creating`, or `task.uploading`
2424
* properties for truthy value when the user tries to navigate away from the page.
25-
*
25+
*
2626
* @returns {Promise}
2727
*/
2828
create() {
2929
// wraps `this.uploading` in a promise that resolves immediately if it is `null` or waits for the promise
3030
return this.cache('creating', () =>
3131
$q.when(this.uploading)
3232
.then( () => $http.post('/api/tasks', this) ) // uploading callback
33-
.then( response => Object.assign(this, response.data) ) // creating callback
3433
);
3534
}
3635

@@ -44,19 +43,18 @@ module.factory( 'Task', (BaseObject, $http) => {
4443
return this.cache('updating', () =>
4544
$q.when(this.uploading)
4645
.then( () => $http.put(`/api/tasks/${this.id}`, this) ) // uploading callback
47-
.then( response => Object.assign(this, response.data) ) // creating callback
4846
);
4947
}
50-
48+
5149
/**
5250
* Allow you to upload attachments to tasks
53-
*
51+
*
5452
* @note Added to demonstrate clean ways to have 1 method wait for another method to finish
5553
* @param {mixed} attachment
5654
* @returns {Promise}
5755
*/
5856
upload(attachment) {
59-
return this.cache('uploading', () =>
57+
return this.cache('uploading', () =>
6058
$http.post(`/api/attachments`, attachment)
6159
.then( response => this.attachments = response.data )
6260
);

0 commit comments

Comments
 (0)