import { Component } from '@angular/core';
import { NavController, AlertController, LoadingController, Loading, IonicPage } from 'ionic-angular';
import { AuthService } from '../../providers/auth-service/auth-service';
/**
* Login Page
*/
@IonicPage()
@Component({
selector: 'page-login',
templateUrl: 'login.html',
})
export class LoginPage {
public loading: Loading;
public userCredentials = {firma: '', username: '', password: '' };
/**
* Konstruktor
*/
constructor(private nav: NavController, private auth: AuthService, private alertCtrl: AlertController, private loadingCtrl: LoadingController) { }
/**
* login button clicked
*/
public createAccount(): void {
this.nav.push('GebmanPage');
}
/**
* login button clicked
*/
public login(): void {
this.showLoading();
this.auth.login(this.userCredentials, {onLoginSuccess: () => {
this.loading.dismiss();
this.nav.setRoot('TabsPage');
}, onLoginUnsuccess: (error) => {
this.loading.dismiss();
this.showError(error);
}})
}
/**
* loading screen is beeing presented
*/
public showLoading(): void {
this.loading = this.loadingCtrl.create({
content: 'Please wait...',
dismissOnPageChange: true
});
this.loading.present();
}
/**
* presents an error alert
*
* @param text
*/
public showError(text): void {
let alert = this.alertCtrl.create({
title: 'Fehler',
subTitle: text,
buttons: ['OK']
});
alert.present(prompt);
}
}
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import {Http, Headers, RequestOptions} from '@angular/http';
/**
* User Model
*/
export class User {
public firma: string;
public username: string;
public password: string;
/**
* Konstruktor
*/
constructor(firma: string, username: string, password: string) {
this.firma = firma;
this.username = username;
this.password = password;
}
}
/**
* Authorization Service
*/
@Injectable()
export class AuthService {
public currentUser: User;
// why no boolean ?
public access: string;
public url: string = "http://127.0.0.1:8888/gebman/api/index.cfm?endpoint=/authenticate";
/**
* Konstruktor
*/
constructor(private http: Http) { }
/**
* user login
*
* @param userCredentials
* @returns {any}
*/
public login(userCredentials: any, functions: any): any {
// haven't checked if && || && is right
if (userCredentials.username && !userCredentials.username.trim() || userCredentials.password && !userCredentials.password.trim()) {
return new Error("Keine Daten");
} else {
let body: any = JSON.stringify({
username: userCredentials.username,
password: userCredentials.password
});
let options: RequestOptions = this.getRequestOptions();
this.getDataFromGebman(body, options)
.subscribe(response => {
this.access = response.ACCESS;
localStorage.userId = response.ACCESS;
if (response.ACCESS == "true") {
functions.onLoginSuccess();
} else {
functions.onLoginUnsuccess("keine Berechtigung");
}
}, error => {
functions.onLoginUnsuccess("Keine Verbindung zum Server.");
});
}
}
/**
* http login call
*
* @param body
* @param options
* @returns {Observable<any>}
*/
public getDataFromGebman(body, options): Observable<any> {
return this.http.post(this.url, body, options)
.map(response => response.json());
}
/**
* http call default header
* @returns {any}
*/
public getRequestOptions(): RequestOptions {
var headers = new Headers();
headers.append("Accept", 'application/json');
headers.append('Content-Type', 'application/json');
let options = new RequestOptions({headers: headers});
return options;
}
/**
* user logout
*/
public logout(): void {
this.currentUser = null;
// how about some localstorage / cookies ?
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.