Created
March 25, 2014 17:12
-
-
Save chatman-media/9766558 to your computer and use it in GitHub Desktop.
Deal with authentication in AngularJS from http://net-daylight.blogspot.ru/2013/11/deal-with-authentication-in-angularjs.html formatted to coffeeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| myApp.controller "AccountController", AccountController = ($scope, $cookies, $log, $location, authenticationService, toaster, userService) -> | |
| $scope.login = (userData, loginForm) -> | |
| if loginForm.$valid | |
| authenticationService.login(userData, $scope.antiForgeryToken).success((response) -> | |
| if response.status | |
| userService.user.username = response.data.userName | |
| userService.user.isLogged = response.data.isLogged | |
| toaster.pop "success", "You are signed in!", "", 2000, true | |
| $location.path "/MembersPage" | |
| else | |
| toaster.pop "error", "Invalid username or password!", "", 2000, true | |
| ).error (data, status, headers, config) -> | |
| $log.info data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| myApp = angular.module("myApp", [ | |
| "ngResource" | |
| "ngCookies" | |
| "ngRoute" | |
| ]) | |
| myApp.config ($httpProvider, $routeProvider) -> | |
| window.routes = | |
| "/Login": | |
| templateUrl: "/Account/Login" | |
| controller: "AccountController" | |
| access: | |
| allowAnonymous: true | |
| "/MembersPage": | |
| templateUrl: "/Home/SomePage,\n controller: SomePageController" | |
| access: | |
| allowAnonymous: false | |
| for path of window.routes | |
| $routeProvider.when path, window.routes[path] | |
| $routeProvider.otherwise redirectTo: "/Login" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| myApp.factory "authenticationService", ($http, $log, $location) -> | |
| login: (login, antiForgeryToken) -> | |
| $http | |
| method: "POST" | |
| url: "/api/Account/AuthenticateUser" | |
| data: login | |
| headers: | |
| RequestVerificationToken: antiForgeryToken | |
| logout: -> | |
| $http.post "/api/Account/Logout" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //in Login.cshtml | |
| @model Model.LoginModel | |
| @{ Layout = null; } | |
| @functions { | |
| private String GetAntiForgeryToken() | |
| { | |
| string cookieToken, formToken; | |
| AntiForgery.GetTokens(null, out cookieToken, out formToken); | |
| return cookieToken + ":" + formToken; | |
| } | |
| } | |
| <div class="container"> | |
| <form name="loginForm" class="form-signin"> | |
| <input id="antiForgeryToken" | |
| data-ng-model="antiForgeryToken" | |
| data-ng-init="antiForgeryToken='@GetAntiForgeryToken()'" type="hidden"/> | |
| <h2 class="form-signin-heading">Authentication</h2> | |
| <br /> | |
| <input type="email" required="required" name="username" class="form-control" | |
| ng-model="userData.username" placeholder="Email address" /> | |
| <input type="password" required name="password" class="form-control" | |
| ng-model="userData.password" placeholder="Password" /> | |
| <label class="checkbox"> | |
| <input type="checkbox" value="remember-me">Remember me</label> | |
| <button type="submit" class="btn btn-lg btn-primary btn-block" ng-click="login(userData, loginForm)">Login</button> | |
| </form> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| myApp.controller "RootController", ($scope, $route, $routeParams, $location, $rootScope, authenticationService, userService, toaster) -> | |
| $scope.user = userService.user | |
| $scope.$on "$routeChangeStart", (e, next, current) -> | |
| $location.path "/Login" if next.access isnt `undefined` and not next.access.allowAnonymous and not $scope.user.isLogged | |
| $scope.logout = -> | |
| authenticationService.logout().success (response) -> | |
| userService.reset() | |
| toaster.pop "info", "You are logged out.", "" | |
| $rootScope.$on "$locationChangeStart", (event, next, current) -> | |
| for i of window.routes | |
| unless next.indexOf(i) is -1 | |
| if not window.routes[i].access.allowAnonymous and not userService.user.isLogged | |
| toaster.pop "error", "You are not logged in!", "" | |
| $location.path "/Login" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| myApp.factory "userService", -> | |
| user = | |
| isLogged: false | |
| username: "" | |
| reset = -> | |
| user.isLogged = false | |
| user.username = "" | |
| user: user | |
| reset: reset |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment