hallo there,
i have created a rest service for user login, register and logout. this one:
and i have tested it with Advanced Rest Client and it is working very well.
I try to write the code on client side but it seems something is getting wrong as i am a new to this one.
these are my files
app.js
var app = angular.module('myApp', ['ionic','ngCordova','ngRoute', 'ngAnimate', 'toaster'])
app.config(['$routeProvider',
function ($routeProvider) {
$routeProvider.
when('/login', {
title: 'Login',
templateUrl: 'partials/login.html',
controller: 'authCtrl'
})
.when('/logout', {
title: 'Logout',
templateUrl: 'partials/login.html',
controller: 'logoutCtrl'
})
.when('/signup', {
title: 'Signup',
templateUrl: 'partials/signup.html',
controller: 'authCtrl'
})
.when('/dashboard', {
title: 'Dashboard',
templateUrl: 'partials/dashboard.html',
controller: 'authCtrl'
})
.when('/', {
title: 'Login',
templateUrl: 'partials/login.html',
controller: 'authCtrl',
role: '0'
})
.otherwise({
redirectTo: '/login'
});
}])
.run(function ($rootScope, $location, Data) {
$rootScope.$on("$routeChangeStart", function (event, next, current) {
$rootScope.authenticated = false;
Data.get('session').then(function (results) {
if (results.uid) {
$rootScope.authenticated = true;
$rootScope.uid = results.uid;
$rootScope.name = results.name;
$rootScope.email = results.email;
} else {
var nextUrl = next.$$route.originalPath;
if (nextUrl == '/signup' || nextUrl == '/login') {
} else {
$location.path("/login");
}
}
});
});
});
authCtrl.js
app.controller('authCtrl', function ($scope, $rootScope, $routeParams, $location, $http, Data) {
//initially set those objects to null to avoid undefined error
$scope.login = {};
$scope.register = {};
$scope.doLogin = function () {
Data.post('login', {
}).then(function (results) {
Data.toast(results);
if (results.status == "success") {
$location.path('dashboard');
}
});
};
$scope.register = {name:'',email:'',password:''};
$scope.Register = function (register) {
Data.post('register', {
name: register.name,
email: register.email,
password: register.password
}).then(function (results) {
Data.toast(results);
if (results.status == "success") {
$location.path('dashboard');
}
});
};
$scope.logout = function () {
Data.get('logout').then(function (results) {
Data.toast(results);
$location.path('login');
});
}
});
signup.html
<div class="breadcrumbs" id="breadcrumbs">
<ul class="breadcrumb">
<li>
<a href="#">Home</a>
</li>
<li class="active">SignUp</li>
</ul>
</div>
<div class="page-content">
<div class="row">
<div class="space-6"></div>
<div class="col-sm-6 col-sm-offset-1">
<div id="login-box" class="login-box visible widget-box no-border">
<div class="widget-body">
<div class="widget-main">
<form name="signupForm" class="form-horizontal" role="form">
<div class="form-group">
<label class="col-sm-5 control-label no-padding-right" for="name">Name</label>
<div class="col-sm-7">
<span class="block input-icon input-icon-right">
<input type="text" class="form-control" placeholder="Name" ng-model="register.name" />
</span>
</div>
</div>
<div class="form-group">
<label class="col-sm-5 control-label no-padding-right" for="email">Email</label>
<div class="col-sm-7">
<span class="block input-icon input-icon-right">
<input type="text" class="form-control" placeholder="Email" name="email" ng-model="register.email" focus/>
<span ng-show="signupForm.email.$error.email" class="help-inline">Email is not valid</span>
</span>
</div>
</div>
<div class="form-group">
<label class="col-sm-5 control-label no-padding-right" for="password">Password</label>
<div class="col-sm-7">
<span class="block input-icon input-icon-right">
<input type="password" class="form-control" name = "password" placeholder="Password" ng-model="register.password" required/>
<small class="errorMessage" data-ng-show="signupForm.password.$dirty && signupForm.password.$invalid"> Enter Password.</small>
</span>
</div>
</div>
<div class="form-group">
<label class="col-sm-5 control-label no-padding-right" for="password2">Confirm Password</label>
<div class="col-sm-7">
<span class="block input-icon input-icon-right">
<input type="password" class="form-control" name="password2" placeholder="Password Again" ng-model="register.password2" password-match="register.password" required/>
<small class="errorMessage" data-ng-show="signupForm.password2.$dirty && signupForm.password2.$error.required"> Enter password again.</small>
<small class="errorMessage" data-ng-show="signupForm.password2.$dirty && signupForm.password2.$error.passwordNoMatch && !signupForm.password2.$error.required"> Password do not match.</small>
</span>
</div>
</div>
<div class="form-group">
<span class="lbl col-sm-5"> </span>
<div class="col-sm-7">
<button type="submit" class="width-35 pull-right btn btn-sm btn-primary" ng-click="Register(register)" data-ng-disabled="signupForm.$invalid">
Sign Up
</button>
</div>
</div>
</fieldset>
<span class="lbl col-sm-5"> </span>
<div class="col-sm-7">Already have an account? <a href="#/login">SignIn</a>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
i have found solution for the CORS issue by adding the headers in apache server so the problems is not there.
i have always the error of the topic:
XMLHttpRequest cannot load http://example.co/task_manager/v1/register. Invalid HTTP status code 404
and also
OPTIONS http://example.com/task_manager/v1/register ionic.bundle.js:18654
i am afraid about authCtrl.js about the register function.
thank you