Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- angular.module('login').
- component('login',{
- templateUrl: '/api/templates/login.html',
- controller: function(User,$cookies, $http, $location, $routeParams, $scope) {
- // var loginUrl = '/api/auth/token/'
- var loginUrl = '/api/user/login/'
- // $http.post(
- $scope.user = {}
- var tokenExists = $cookies.get("token")
- if (tokenExists){
- $scope.loggedIn = true;
- $cookies.remove("token")
- $scope.user = {
- username: $cookies.get("username")
- }
- //necessario dar o refresh na pagina pois o cookie fica ainda armazenado mesmo apos logoff
- window.location.reload()
- }
- $scope.loginError = {}
- $scope.$watch(function(){
- if($scope.user.password){
- $scope.loginError.password = ""
- }
- if ($scope.user.username){
- $scope.loginError.username = ""
- }
- })
- $scope.doLogin = function(user){
- //para não precisar acessar o servidor
- if (!user.username){
- $scope.loginError.username = ["Field required"]
- }
- if (!user.password){
- $scope.loginError.password = ["Field required"]
- }
- if (user.username && user.password){
- var reqConfig = {
- method:"POST",
- url: loginUrl,
- data:{
- username: user.username,
- password: user.password
- },
- headers: {},
- }
- // var requestAction = $http.post(loginUrl,user)
- var requestAction = $http(reqConfig)
- requestAction.success(function(r_data,r_status,r_headers,r_config){
- //inserindo o cookie que veio no response dentro do $cookies usando put
- // console.log(r_data)
- $cookies.put("id",r_data.id)
- $cookies.put("token",r_data.token)
- $cookies.put("username",r_data.username)
- $cookies.put("aplicativos",r_data.aplicativos)
- $cookies.put("admin",r_data.admin)
- // window.location.reload()
- //redireciona pasra pagina após login
- $location.path("/dashboard")
- // window.location.reload()
- })
- requestAction.error(function(e_data,e_status,e_headers,e_config){
- console.log(e_data)//token
- $scope.loginError = e_data
- })
- }
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement