###What Is NG-Token-Auth
NG-Token-Auth is a client side module for angular. This module is designed to work out-of-the-box with Devise-Token-Auth.
IF you are using jquery, you can use jToker
###Installing NG-Token-Auth
steps:
- run
bower install ng-token-auth --save
- In
app/assets/javascripts/application.js
3. add//= require ng-token-auth/dist/ng-token-auth
4. and make sure you have//= require angular-cookie/angular-cookie
###Configuring NG-Token-Auth
In app/assets/javascripts/angular/app.js.erb
- add
'ng-token-auth'
toangular.module
- add
'$authProvider'
toapp.config
- inside
app.config
, add
$authProvider.configure({
// By default, you only need to configure apiUrl
// Note that if you put a '/' at the end of the link, there will be errors when calling the api
apiUrl: 'http://localhost:3000'
})
steps:
- in
app/assets/javascripts/angular/app.js.erb
, set new routes forsignup
that refers toSignUpCtrl
and the corresponding HTML file
when('/', {
templateUrl: "<%= asset_path('static_pages/index.html') %>",
controller: 'StaticPagesCtrl'
}).
when('/signup', {
templateUrl: "<%= asset_path('auth/signup.html') %>",
controller: 'SignUpCtrl'
})
- in
app/assets/javascripts/angular/controllers
, createsignup.js.erb
andlogin.js.erb
app.controller('SignUpCtrl', ['$scope', function($scope){
}])
- in
app/assets/templates/auth
, createsignup.html
<div class="container">
<div class="row">
<h1>Sign Up</h1>
</div>
</div>
- in
app/assets/templates/static_pages/index.html
add a button redirecting to/#/signup
<div class="container">
<div class="row">
<h1> {{ message }} </h1>
</div>
<div class="row">
<a href="/#/signup"><button class="btn btn-primary">Signup</button></a>
</div>
</div>
steps:
- In
signup.js.erb
add$auth
and$location
- add this code
$scope.signup = function () {
$auth.submitRegistration($scope.registrationForm).
then(function(resp) {
// handle success response
console.log(resp);
// redirect back to root when registration succesfull
$location.path('/');
}).
catch(function(resp) {
// handle error response
console.log(resp);
});
};
- in
signup.html
add a form. You will need three inputsemail
,password
, andpassword_confirmation
<div class="container">
<div class="row">
<h1>Sign Up</h1>
<form ng-submit="signup()" role="form" ng-init="registrationForm = {}">
<div class="form-group">
<label>Email</label>
<input type="email" name="email" ng-model="registrationForm.email" required="required" class="form-control" placeholder="Enter Email"/>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" ng-model="registrationForm.password" required="required" class="form-control" placeholder="Enter Password"/>
</div>
<div class="form-group">
<label>password Confirmation</label>
<input type="password" name="password_confirmation" ng-model="registrationForm.password_confirmation" required="required" class="form-control" placeholder="Enter Password Confirmation"/>
</div>
<button type="submit" class="btn btn-primary btn-lg">Register</button>
<a href="/#/"><button class="btn btn-danger btn-lg">Back</button></a>
</form>
</div>
</div>
###Logout
- in
app/assets/angular/controllers/static_pages.js.erb
add this code
$scope.logout = function () {
$auth.signOut().then(function(resp) {
console.log(resp);
}).catch(function(resp) {
console.log(resp);
});
};
- in
app/assets/templates/static_pages/index.html
add a button that calls the logout function when clicked
<button class="btn btn-danger" ng-click="logout()">Logout</button>
###Login
Repeat the steps in Setting Up Routes and Using Ng-Token-Auth For Signup with the following changes
- in
login.js.erb
, instead of using$auth.submitRegistration
use$auth.submitLogin
- in
login.js.erb
, instead of using$scope.registrationForm
use$scope.loginForm
- in
login.html
, instead of having three inputsemail
,password
, andpassword_confirmation
use onlyemail
andpassword
- in
login.html
, instead of usingregistrationForm
useloginFrom
###Extra Information. There are more stuff you can do!
Aside from signup, login, and logout, there is actually many more functions like forget password, reset password, facebook login...etc. For more read this
Aside from functions, there are events like auth:login-success
. Using these, will be able to know when a user login even if you are not in the login controller! For more read this