added error messages for login, after login: nav changes and home gets opend

master
Max 5 years ago
parent eeb339b1fa
commit d7bd80b4d6

@ -1,5 +1,6 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { LoginComponent } from '../login/login.component'; import { LoginComponent } from '../login/login.component';
import { DatasharingService } from '../../services/datasharing.service';
@Component({ @Component({
selector: 'app-scaffold', selector: 'app-scaffold',
@ -8,10 +9,14 @@ import { LoginComponent } from '../login/login.component';
}) })
export class AppScaffoldComponent implements OnInit { export class AppScaffoldComponent implements OnInit {
loggedIn: boolean = false;
constructor() { } constructor(private data: DatasharingService) { }
ngOnInit() { ngOnInit() {
this.data.currentUserInfo.subscribe(user => {
this.loggedIn = user.loggedIn;
console.log('he´s comming through!');
})
} }
} }

@ -12,6 +12,7 @@
</tr> </tr>
</table> </table>
<button type="loginbutton" (click)="onClickSubmit(email.value,password.value)">Login</button> <button type="loginbutton" (click)="onClickSubmit(email.value,password.value)">Login</button>
<p *ngIf="errorOccurred">{{errorMessage}}</p>
<br> <br>
<a href="/register">You aren´t part of greenvironment yet? - join us here</a> <a href="/register">You aren´t part of greenvironment yet? - join us here</a>
</div> </div>

@ -1,6 +1,8 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Login } from 'src/app/models/login'; import { Login } from 'src/app/models/login';
import { LoginService } from 'src/app/services/login/login.service'; import { LoginService } from 'src/app/services/login/login.service';
import { RouterLink } from '@angular/router';
import {Router} from '@angular/router';
@Component({ @Component({
selector: 'app-login', selector: 'app-login',
@ -9,17 +11,27 @@ import { LoginService } from 'src/app/services/login/login.service';
}) })
export class LoginComponent implements OnInit { export class LoginComponent implements OnInit {
login: Login login: Login
errorOccurred: boolean = false;
errorMessage: string;
constructor(private loginService: LoginService) { constructor(private loginService: LoginService,private router: Router) {
this.login = {passwordHash: null, email: null}; this.login = {passwordHash: null, email: null};
} }
public loginError(error : any){
console.log(error.errors[0].message);
this.errorOccurred = true;
this.errorMessage = error.errors[0].message;
}
onClickSubmit(pEmail: string, pPasswordHash: string) { onClickSubmit(pEmail: string, pPasswordHash: string) {
console.log('email: ' + pEmail); console.log('try to login with mail adress:' + pEmail);
this.errorOccurred = false;
this.errorMessage = " ";
this.login.email = pEmail this.login.email = pEmail
this.login.passwordHash = pPasswordHash this.login.passwordHash = pPasswordHash
this.loginService.login(this.login) this.loginService.login(this.login, error => this.loginError(error.json()));
} }
ngOnInit() {} ngOnInit() {}

@ -4,15 +4,16 @@ import { Login } from '../../models/login';
import { User } from 'src/app/models/user'; import { User } from 'src/app/models/user';
import { DatasharingService } from '../datasharing.service'; import { DatasharingService } from '../datasharing.service';
import { userInfo } from 'os'; import { userInfo } from 'os';
import {Router} from '@angular/router';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class LoginService { export class LoginService {
constructor(private http: Http, private data: DatasharingService) { } constructor(private http: Http, private data: DatasharingService,private router: Router) { }
public login(login : Login) { public login(login : Login, errorCb: any) {
//let url = './graphql' //let url = './graphql'
let url = 'https://greenvironment.net/graphql' let url = 'https://greenvironment.net/graphql'
@ -23,8 +24,15 @@ export class LoginService {
return this.http.post(url, this.buildJson(login)) return this.http.post(url, this.buildJson(login))
.subscribe(response => { .subscribe(response => {
console.log(response.text()); console.log(response.text());
this.loginSuccess();
this.updateUserInfo(response.json()) this.updateUserInfo(response.json())
}); }, errorCb
);
}
public loginSuccess(){
console.log('alles supi dupi');
//do routing
this.router.navigateByUrl('');
} }
public updateUserInfo(response : any){ public updateUserInfo(response : any){

Loading…
Cancel
Save