Skip to content

Commit 8074169

Browse files
committed
Getting authenticated user and updating scope.
1 parent c96dceb commit 8074169

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

app/Http/Controllers/UserController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public function store()
8383
if (!$user->save()) {
8484
abort(500, 'Could not save user.');
8585
}
86+
$user['token'] = $this->jwtAuth->fromUser($user);
8687
return $user;
8788
}
8889

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
angular.module('MainController', []).controller('MainController', ['$scope', '$location',
2-
function ($scope, $location) {
1+
angular.module('MainController', []).controller('MainController', ['$scope', '$location', '$localStorage', 'User',
2+
function ($scope, $location, $localStorage, User) {
33
/**
44
* Responsible for highlighting the currently active menu item in the navbar.
55
*
@@ -9,5 +9,29 @@ angular.module('MainController', []).controller('MainController', ['$scope', '$l
99
$scope.isActive = function (route) {
1010
return route === $location.path();
1111
};
12+
13+
$scope.getAuthenticatedUser = function (user) {
14+
if (user) {
15+
$scope.authenticatedUser = user;
16+
return;
17+
}
18+
19+
if (typeof $localStorage.token === 'undefined') {
20+
return null;
21+
}
22+
23+
new User().$getByToken(function (user) {
24+
console.log(user);
25+
$scope.authenticatedUser = user;
26+
}, function (err) {
27+
//console.log(err);
28+
//$scope.authenticatedUser = err;
29+
});
30+
};
31+
32+
$scope.logout = function () {
33+
delete $localStorage.token;
34+
$scope.authenticatedUser = null;
35+
};
1236
}
1337
]);

resources/js/controllers/UserController.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
angular.module('UserController', []).controller('UserController', ['$scope', '$rootScope', 'User', 'Auth', '$localStorage',
2-
function ($scope, $rootScope, User, Auth, $localStorage) {
31
angular.module('UserController', []).controller('UserController', ['$scope', '$rootScope', 'User', 'Auth', '$localStorage', '$location',
42
function ($scope, $rootScope, User, Auth, $localStorage, $location) {
53
$scope.login = function () {
@@ -9,6 +7,7 @@ angular.module('UserController', []).controller('UserController', ['$scope', '$r
97
});
108
user.$login(function (user) {
119
$localStorage.token = user.token;
10+
$scope.getAuthenticatedUser(user);
1211
}, function (err) {
1312
console.log(err);
1413
});
@@ -23,14 +22,24 @@ angular.module('UserController', []).controller('UserController', ['$scope', '$r
2322
password: this.password,
2423
email: this.email
2524
});
26-
user.$save(function (res) {
27-
$location.path('users/view/' + res.id);
25+
user.$save(function (user) {
26+
$localStorage.token = user.token;
27+
$scope.getAuthenticatedUser(user);
28+
$location.path('users/view/' + user.id);
29+
}, function (err) {
30+
console.log(err);
31+
});
32+
};
2833

2934
$scope.findOne = function () {
3035
var splitPath = $location.path().split('/');
3136
var userId = splitPath[splitPath.length - 1];
3237
$scope.user = User.get({userId: userId});
3338
};
39+
40+
$scope.getByToken = function () {
41+
return new User().$getByToken(function (res) {
42+
console.log(res);
3443
}, function (err) {
3544
console.log(err);
3645
});

resources/views/layout.blade.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<link rel="stylesheet" href="/css/app.css"/>
99
<link rel="stylesheet" href="<% elixir('css/all.css') %>"/>
1010
</head>
11-
<body ng-app="todoApp" ng-controller="MainController">
11+
<body ng-app="todoApp" ng-controller="MainController" ng-init="getAuthenticatedUser()">
1212

1313
<nav class="navbar navbar-inverse navbar-fixed-top">
1414
<div class="container">
@@ -25,8 +25,10 @@
2525
<ul class="nav navbar-nav">
2626
<li ng-class="{active:isActive('/todos')}"><a href="/todos">List Todos</a></li>
2727
<li ng-class="{active:isActive('/todos/create')}"><a href="/todos/create">Create Todo</a></li>
28-
<li ng-class="{active:isActive('/auth/signup')}"><a href="/auth/signup">Sign Up</a></li>
29-
<li ng-class="{active:isActive('/auth/login')}"><a href="/auth/login">Log in</a></li>
28+
<li ng-if="authenticatedUser == null" ng-class="{active:isActive('/auth/signup')}"><a href="/auth/signup">Sign Up</a></li>
29+
<li ng-if="authenticatedUser == null" ng-class="{active:isActive('/auth/login')}"><a href="/auth/login">Log in</a></li>
30+
<li ng-if="authenticatedUser != null" ng-class="{active:isActive('/users/view/' + authenticatedUser.id)}"><a ng-href="/users/view/{{authenticatedUser.id}}">{{authenticatedUser.username}}</a></li>
31+
<li ng-if="authenticatedUser != null" ng-click="logout()"><a ng-href="#">Log out</a></li>
3032
</ul>
3133
</div>
3234
</div>

resources/views/partials/index.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
This is the index.
1+
<p ng-if="authenticatedUser">
2+
Hello {{authenticatedUser.username}}, thank you for installing me.
3+
</p>
4+
<p ng-if="!authenticatedUser">
5+
Hello guest, thank you for installing me.
6+
</p>

0 commit comments

Comments
 (0)