0

I want to get User after login to my app to create his own account. For example I want his Id to redirect him to page: '/user-cars/:userId'. I'm using Angular JS and SpringBoot. I dont know how to do it. Below is my code:

Config.js

angular.module('app')
.config(function ($routeProvider, $httpProvider) {
    $routeProvider
        .when('/users', {
            templateUrl: 'app/components/users/list/userList.html',
            controller: 'UserListController',
            controllerAs: 'ctrl'
        })
        .when('/user-add', {
            templateUrl: 'app/components/users/edit/userEdit.html',
            controller: 'UserEditController',
            controllerAs: 'ctrl'
        })

        .when('/user-cars/:userId', {
            templateUrl: 'app/components/users/userCars/carList.html',
            controller: 'CarListController',
            controllerAs: 'ctrl'
        })
        .when('/user-login',{
            templateUrl: 'app/components/users/login/login.html',
            controller: 'AuthenticationController',
            controllerAs: 'ctrl'
        })

        .otherwise({
            redirectTo: '/',
            controller: 'HomeController',
            controllerAs: 'ctrl'
        });
    $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
});

authService.js

angular.module('app')
.constant('LOGIN_ENDPOINT', '/user-login')
.constant('LOGOUT_ENDPOINT', '/user-logout')
.service('AuthenticationService', function($http, LOGIN_ENDPOINT, LOGOUT_ENDPOINT) {
    this.authenticate = function(credentials, successCallback) {
        var authHeader = {Authorization: 'Basic ' + btoa(credentials.email+':'+credentials.password)};
        var config = {headers: authHeader};
        $http
            .post(LOGIN_ENDPOINT, {
            }, config)
            .then(function success(res) {
                $http.defaults.headers.post.Authorization = authHeader.Authorization;
                successCallback();
            }, function error(reason) {
                console.log('Login error');
                console.log(reason);
            });
    }
    this.logout = function(successCallback) {
        delete $http.defaults.headers.post.Authorization;
            successCallback();
    }

});

authController.js

angular.module('app')
.controller('AuthenticationController', function($rootScope, $location, AuthenticationService) {
    var vm = this;
    vm.credentials = {};
    var loginSuccess = function() {
        $rootScope.authenticated = true;
        $location.path('/');
    }
    vm.login = function() {
        AuthenticationService.authenticate(vm.credentials, loginSuccess);
    }
    var logoutSuccess = function() {
        $rootScope.authenticated = false;
        $location.path('/');
    }
    vm.logout = function() {
        AuthenticationService.logout(logoutSuccess);
    }
})

All is working, I got principal, from Back-end, but now I dont know how to use it and get user

RestController.java`

@RestController
public class AuthenticationController { 
    @RequestMapping("/user-login")
    @ResponseBody
    public Principal login(Principal user)
    {
        return user;
    }`

Authorization works, I got principal, but I dont know what I have to do with him.

Piotron
  • 3
  • 2

1 Answers1

0

Based on your UserDetails implementation you can get the user details populated. The default implementation has only username and password, additional details can be added to a custom implementation.

Example from Spring reference documentation

Authentication auth = httpServletRequest.getUserPrincipal();
// assume integrated custom UserDetails called MyCustomUserDetails
// by default, typically instance of UserDetails
MyCustomUserDetails userDetails = (MyCustomUserDetails) auth.getPrincipal();
String firstName = userDetails.getFirstName();
String lastName = userDetails.getLastName();

Go through the comments , default implementation is of UserDetails

example for custom implementation for your purpose would be

public class MyCustomUserDetails implements UserDetails{
 // Have the id field declared
 private String userId;
 // ...getters and setters

}

A complete example can be found with this SO question and answers.

R.G
  • 6,436
  • 3
  • 19
  • 28
  • thanks for help, do you know how can I take it over in angular controller? – Piotron Mar 19 '20 at 13:49
  • The request reaches the `AuthenticationController` after the spring security framework authenticates the principal. Which means the authentication has already happened and the principal is set. If you need custom userdetails you will need to add the required logic to be performed during the actual authentication.Please read through the reference [documentation](https://docs.spring.io/spring-security/site/docs/current/reference/html5/#servlet-architecture) . There are a lot of examples available online – R.G Mar 19 '20 at 14:01