Closed harryy2510 closed 8 years ago
In every route except login, register, forgot, reset password, and the abstract one wich is the parent one set data: { auth: true }
@flick36 Hi, i have already did that part of authenticating routes, but i want to refresh user data in local storage or session storage everytime the app loads and then use that data in page
Yeah, i answer before you edit your post...
Well it's gonna be hard since you are using your own logic to authenticate an user, i do not know what your auth/checkLogin
API route does :/ or why do you even have the auth
state.
Maybe this can help you: https://github.com/jadjoubran/laravel5-angular-material-starter/pull/297
What i do to accomplish what you want to do is this: https://github.com/jadjoubran/laravel5-angular-material-starter/issues/329#issuecomment-224057736
auth/checkLogin
public function checkLogin()
{
try {
if (! $user = JWTAuth::parseToken()->authenticate()) {
return response()->json(['user_not_found'], 400);
}
} catch (TokenExpiredException $e) {
return response()->json(['token_expired'], 400);
} catch (TokenInvalidException $e) {
return response()->json(['token_invalid'], 400);
} catch (JWTException $e) {
return response()->json(['token_absent'], 400);
}
$user->role = Role::find($user->role_id)->first()->name;
unset($user->roles);
unset($user->role_id);
return response()->json(compact('user'));
}
auth state is just the name for the state where all the routes are accessed only after authentication. name won't matter i will change it later on
the main issue is when i have to activate a user account (link sent to email), and if the satellizer token is valid (which means user is already logged in, which i am checking in accountservice), user should be redirected to dashboard page instead of letting him send the activation account request
Like i said, take a look at https://github.com/jadjoubran/laravel5-angular-material-starter/pull/297 i think that's what you want, basically what this PR does, is to redirect the user if its a;ready logged in therefore, when the token is valid, with this PR you can implement your own logic in routes.config.js
lets take an example...
Your
resolve:{
checkLogin: function(AccountService){
return AccountService.checkLogin();
}
},
works? i mean if you console.log(AccountService.checkLogin())
it returns what expected?
and how are you acquiring that checkLogin
in your html?
yes.. i tried it with $timeout too.. results are expected.. but the thing is before accountservice is called, view is already loaded
Here's the thing, when you are using Restagular on the resolve, you are returning a promise which is rejected if you dont return an instance, resolve will get resolved even when your request is still loading so maybe if you do this:
resolve: {
checkLogin: ["AccountService", function (AccountService) {
var deferred = $q.defer();
AccountService.checkLogin().then(function(data) {
deferred.resolve(data); // Data should be equal to true in this case
}, function(err) {
deferred.resolve(err); // Err should be equal to false in this case
});
return deferred.promise;
}],
}
If i'm correct this should work... but i have the feeling it won't so try it please and tell me if you get error, you'll probably will
it is not calling the api now :( page is loading and loading but no requests are made to backend
yeah sorry i forgt to inject $q
xD
resolve: {
checkLogin: ["$q","AccountService", function ($q, AccountService) {
var deferred = $q.defer();
AccountService.checkLogin().then(function(data) {
deferred.resolve(data); // Data should be equal to true in this case
}, function(err) {
deferred.resolve(err); // Err should be equal to false in this case
});
return deferred.promise;
}],
}
Does it throws any error? try to console.log() something at the beginning and at the end of the resolve fucntion please
its also working but view is loading before resolve returns the promise.
What view are you loading? can you post the state?
I want to check authentication and save/update user data in local storage every time app is loaded for first time. So i made a service which checks for authentication through token and saves data in local storage and i am running this service in run block.
Now the problem lies here:
AccountService
routes_config