Created
October 1, 2018 05:25
-
-
Save Dev0515/3a20a7fd548855812e8776208c28809c to your computer and use it in GitHub Desktop.
Login page code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { Http, Headers, RequestOptions } from '@angular/http'; | |
import { Router } from '@angular/router'; | |
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms'; | |
import { CosmicService } from '../../services/cosmic.service' | |
import {config} from '../../../config/cosmo.config'@Component({ | |
selector: 'app-login', | |
templateUrl: './login.component.html', | |
styleUrls: ['./login.component.css'] | |
}) | |
export class LoginComponent implements OnInit { loginForm: FormGroup; | |
message = ""; | |
data; constructor(private _http: Http, private fb: FormBuilder, private router: Router, private cosmicService: CosmicService) { this.loginForm = this.fb.group({ | |
'username': ['', Validators.required], | |
'password': ['', Validators.required], }); } | |
/** logging user in */ | |
login() { | |
const credentials = this.loginForm.value; | |
` credentials.read_key = config.read_key | |
this.cosmicService.login(credentials) | |
.subscribe(res => { | |
this.data = res; | |
var jsondata = JSON.parse(this.data._body); | |
if (jsondata.message == "No objects returned.") { | |
return this.message = "username or password is wrong"; | |
} | |
console.log(this.data._body); | |
const userPassword = jsondata.objects[0].metadata.password; | |
if (userPassword == credentials.password) { | |
console.log('login Success'); | |
localStorage.setItem('user', JSON.stringify({ jsondata })); | |
this.router.navigate(["dashboard"]); | |
} | |
else { | |
this.message = "Username or password is wrong"; | |
console.log('Login Failed'); | |
} | |
}) }/** navigate to register page */ | |
registerCall() { | |
this.router.navigate(["register"]); | |
} | |
ngOnInit() { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment