angularjs - Building a Angular Login System with MySQL Database -
i have found many tutorials of angular authentications systems using external api etc. want have user login database of users, in php. how go doing this? controllers/services , directives in 1 file dont know how separate them of yet.
what framework suggest website allow users post projects , such , other usergroups can hired projects? looking nice feel angular. suggestions?
this have far:
login.html:
<div ng-controller="loginctrl"> <h3 style="text-align:center;">{{moduletitle}}</h3> <form class="form-signin" name="loginform" role="form"> <label for="inputemail" class="sr-only">email</label> <input type="text" id="inputemail" class="form-control" placeholder="email address" ng-model="data.email"> <label for="inputpassword" class="sr-only">password</label> <input type="password" id="inputpassword" class="form-control" placeholder="password" ng-model="data.password"> <div class="checkbox"> <label> <input type="checkbox" value="remember-me"> remember me </label> </div> <button type="submit" class="btn btn-lg btn-primary btn-block" ng-click="login()" data-ng-disabled="loginform.$invalid">login</button> </form> </div>
app.js:
var app = angular.module('myapp', [ 'ngroute' ]); app.config(['$routeprovider', function($routeprovider) { $routeprovider. when('/', { templateurl: 'views/login.html', controller: 'loginctrl', title: 'login' }). when('/signup', { templateurl: 'views/signup.html', controller: 'signupctrl', title: 'signup' }). when('/dashboard', { templateurl: 'views/dashboard.html', controller: 'dashctrl', title: 'dashboard' }). otherwise({ redirectto: '/404', templateurl:'views/404.html' }) }]); app.controller('loginctrl', function($scope, authservice, $route, $rootscope) { $scope.data = {}; $rootscope.pagetitle = $route.current.title; $scope.moduletitle = "login firelance"; $scope.login = function() { authservice.loginuser( $scope.data.email, $scope.data.password ).success(function(data) { alert('success'); }).error(function(data) { var alertpopup = alert( 'please check credentials!' ); }); } }) app.service('authservice', function($q) { return { loginuser: function(name, pw) { var deferred = $q.defer(); var promise = deferred.promise; if (name == 'savisaar2' && pw == 'naf44nss') { deferred.resolve('welcome ' + name + '!'); } else { deferred.reject('wrong credentials.'); } promise.success = function(fn) { promise.then(fn); return promise; } promise.error = function(fn) { promise.then(null, fn); return promise; } return promise; } } })
since you're using angularjs front-end framework here, have use form of api , not directly access database. don't know of javascript in-browser mysql dbals anyway, , on top of that, if there is/was 1 you'd have store database credentials in plain text , send them using application part of application (not idea!). on top of that, database have open external access (also not idea!).
controllers manipulate view. assign methods , properties on scope, , available in view. can use methods , properties in view , manipulate them.
directives reusable blocks. can put specific bits of logic in them, have them interact services, , work view , controller described above.
services real business logic should go, should call upon services in controllers / directives do things. like, example, have service api client, , uses api client handle area of api, or maybe have service processing data show in chart, or whatever. idea is, services reusable, testable, , mockable. if put logic controller, not reusable.
Comments
Post a Comment