How to get the authenticated user info and use in all controllers and services?

vzhen

I'm using angularFireAuth and I want to retrieve the logged in user's info and use in all the controllers or services when the app is initial.

Currently, I used this in every controller but i having some problem.

$scope.$on("angularFireAuth:login", function(evt, user){ 
  console.log(user);
});

The callback will not call if it is not a full page load or return null when app initial.

I need some tips for how can I return the authenticated user's info so I can use when app is initial and in all the controllers and services.

Example

When in controller or services

  • $scope.auth.user.id will return user's ID
  • $scope.auth.user.name will return user's name
  • etc
manojlds

I would start with a userService for this:

angular.module('EventBaseApp').service('userService', function userService() {
    return {
        isLogged: false,
        username: null
    }

});

And write a LoginCtrl controller:

angular.module('EventBaseApp')
  .controller('LoginCtrl', function ($scope, userService, angularFireAuth) {
    var url = "https://example.firebaseio.com";
    angularFireAuth.initialize(url, {scope: $scope, name: "user"});

    $scope.login = function() {
        angularFireAuth.login("github");

    };
    $scope.logout = function() {
        angularFireAuth.logout();
    };

    $scope.$on("angularFireAuth:login", function(evt, user) {
      userService.username = $scope.user;
      userService.isLogged = true;
    });

    $scope.$on("angularFireAuth:logout", function(evt) {
      userService.isLogged = false;
      userService.username = null;
    });
});

Inject the userService anywhere you want the user.

My app that am currently working on that uses this - https://github.com/manojlds/EventBase/blob/master/app/scripts/controllers/login.js

Based on ideas presented here - http://blog.brunoscopelliti.com/deal-with-users-authentication-in-an-angularjs-web-app

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get a Cognito authenticated user info in a AWS Lambda function?

OAuth: how to get authenticated user info after access token?

Spring Security get user info in rest service, for authenticated and not authenticated users

How to I use all user info in sessions

symfony2 how to access user credentials/info once authenticated?

how to use get_user_info for Discord.py rewrite

How to get authenticated user in event listener - Laravel

How to get the authenticated user avatar in the header blade?

How to get id of authenticated user in Olingo ODataServiceFactory

How to get authenticated user on serializer class for validation

Laravel - How to get the guard that authenticated the user?

How to get email Id of authenticated user?

how to get authenticated user on API side

How can get user info?

How to check if a user is logged in (how to properly use user.is_authenticated)?

Get all objects' django guardian permissions of the authenticated user in template

laravel get the authenticated user

How to use Firebase transaction to create user collection and Authenticated user?

how to use a controller's action in all controllers

How to use session for all controllers in Laravel 5.6

How to share the authenticated user across all laravel views

How to use redirect_authenticated_user with extra_context in Django

How to use {% if user.is_authenticated %} in html to display or remove content

How to get the authenticated user from a separate class library in .Net Core

How to get the uid of the authenticated Firebase user in a Cloud Functions storage trigger

How to get "validated Body" and "authenticated user" in a request of a NestJS Controller Handler?

How to get authenticated user's screen name with Tweepy

How to get current authenticated user id using Vuejs and Laravel?

how to get data stored for an authenticated user store in a table in a database in laravel