Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {IonicApp, Page, NavController, Events, Alert} from 'ionic-angular';
- import {HomePage} from '../home/home';
- import {SignupPage} from '../signup/signup';
- import {FormBuilder, Validators, FORM_BINDINGS, ControlGroup} from 'angular2/common'
- import {HttpService} from '../../providers/http-service';
- import {ValidationService} from '../../providers/validator-service';
- @Page({
- providers: [HttpService, ValidationService],
- templateUrl: 'build/pages/login/login.html'
- })
- export class LoginPage {
- nav: any;
- events: any;
- http: any;
- login = {};
- submitted = false;
- loginForm: ControlGroup;
- constructor(formBuilder: FormBuilder, nav: NavController, events: Events, http: HttpService) {
- this.nav = nav;
- this.events = events;
- this.http = http;
- // Build login form with validators
- this.loginForm = formBuilder.group({
- username: ["", Validators.compose([Validators.required, ValidationService.emailValidator])],
- password: ["", Validators.compose([Validators.required, ValidationService.passwordValidator])]
- });
- }
- onLogin() {
- this.submitted = true;
- if (this.loginForm.valid) {
- // build the request from the form
- let request = {};
- request['username'] = this.loginForm.value.username;
- request['password'] = this.loginForm.value.password;
- request['accessToken'] = '';
- request['loginMethod'] = 'STANDALONE';
- // make the request
- this.http.makeBackendRequest('POST', 'auth/login', request, this.onLoginSuccess, this.onLoginError, false);
- }
- }
- onLoginSuccess(response) {
- // publish event to update the database
- this.events.publish('user:login', response);
- // show alert to inform user and redirect him to Home
- this.showAlert("Connexion réussi", "Merci d'utiliser notre application, bon networking !", {
- text: 'Ok',
- handler: () => {
- this.nav.setRoot(HomePage);
- }
- });
- }
- onLoginError(errorMessage) {
- let code = errorMessage.status;
- if (typeof code == "undefined")
- LoginPage.prototype.showAlert("Serveur non-accessible", "Notre serveur n'a pas répondu, veuillez réessayez", "Ok");
- else if (code == "404")
- LoginPage.prototype.showAlert("Erreur d'Authentification", "Aucun utilisateur trouvé pour cet email.", "Ok");
- else if (code == "502")
- LoginPage.prototype.showAlert("Serveur non-accessible", "Notre serveur n'a pas répondu, veuillez réessayez", "Ok");
- else if (code == "403")
- LoginPage.prototype.showAlert("Erreur d'Authentification", "Le mot-de-passe est incorrect.", "Ok");
- }
- public showAlert(title, subTitle, button) {
- let alert = Alert.create({
- title: title,
- subTitle: subTitle,
- buttons: [button]
- });
- this.nav.present(alert);
- }
- onSignup() {
- this.nav.setRoot(SignupPage);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement