Skip to content

Commit

Permalink
Implementar el login y registro de usuarios en el FrontEnd
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrotomassini committed Mar 15, 2022
1 parent 4949c96 commit 97a9dc7
Show file tree
Hide file tree
Showing 26 changed files with 697 additions and 556 deletions.
431 changes: 51 additions & 380 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@
"@angular/platform-browser-dynamic": "~10.0.0",
"@angular/router": "~10.0.0",
"chart.js": "^2.9.3",
"mongoose": "^6.2.6",
"ng2-charts": "^2.3.2",
"rxjs": "~6.5.4",
"sweetalert2": "^9.15.2",
"tslib": "^2.0.0",
"zone.js": "~0.10.2"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.1000.0",
"@angular/cli": "~10.0.0",
"@angular/compiler-cli": "~10.0.0",
"@types/node": "^12.11.1",
"@types/jasmine": "~3.5.0",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^12.11.1",
"codelyzer": "^5.1.2",
"jasmine-core": "~3.5.0",
"jasmine-spec-reporter": "~5.0.0",
Expand Down
7 changes: 5 additions & 2 deletions src/app/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';

import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';
Expand All @@ -18,7 +19,9 @@ import { RegisterComponent } from './register/register.component';
imports: [
CommonModule,
RouterModule,
FormsModule
FormsModule,
ReactiveFormsModule,
HttpClientModule
]
})
export class AuthModule { }
30 changes: 22 additions & 8 deletions src/app/auth/login/login.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,35 @@
<div class="login-box card">
<div class="card-body">
<form class="form-horizontal form-material"
(submit)="login()"
id="loginform">
autocomplete="off"
[formGroup]="loginForm"
(ngSubmit)="login()"
id="loginform">

<a href="javascript:void(0)" class="text-center db"><img src="../assets/images/logo-icon.png" alt="Home" /><br/><img src="../assets/images/logo-text.png" alt="Home" /></a>
<div class="form-group m-t-40">
<div class="col-xs-12">
<input class="form-control" type="text" required="" placeholder="Username">
<input class="form-control"
type="email"
placeholder="Email"
formControlName="email">
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<input class="form-control" type="password" required="" placeholder="Password">
<input class="form-control"
type="password"
placeholder="Password"
formControlName="password">
</div>
</div>
<div class="form-group row">
<div class="col-md-12">
<div class="checkbox checkbox-primary pull-left p-t-0">
<input id="checkbox-signup" type="checkbox" class="filled-in chk-col-light-blue">
<input id="checkbox-signup"
type="checkbox"
class="filled-in chk-col-light-blue"
formControlName="remember">
<label for="checkbox-signup"> Remember me </label>
</div>
<a href="javascript:void(0)" id="to-recover" class="text-dark pull-right"><i class="fa fa-lock m-r-5"></i> Forgot pwd?</a> </div>
Expand All @@ -32,13 +44,15 @@
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 m-t-10 text-center">
<div class="social"><a href="javascript:void(0)" class="btn btn-facebook" data-toggle="tooltip" title="Login with Facebook"> <i aria-hidden="true" class="fa fa-facebook"></i> </a> <a href="javascript:void(0)" class="btn btn-googleplus" data-toggle="tooltip" title="Login with Google"> <i aria-hidden="true" class="fa fa-google-plus"></i> </a> </div>
<div class="col-xs-12 col-sm-12 col-md-12 m-t-10 mb-4 text-center" id="google-btn">

<div id="my-signin2"></div>

</div>
</div>
<div class="form-group m-b-0">
<div class="col-sm-12 text-center">
Don't have an account? <a href="pages-register2.html" class="text-primary m-l-5"><b>Sign Up</b></a>
Don't have an account? <a routerLink="/register" class="text-primary m-l-5"><b>Sign Up</b></a>
</div>
</div>
</form>
Expand Down
84 changes: 81 additions & 3 deletions src/app/auth/login/login.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, NgZone } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { UsuarioService } from '../../services/usuario.service';
import Swal from 'sweetalert2';

declare const gapi:any;

@Component({
selector: 'app-login',
Expand All @@ -8,13 +13,86 @@ import { Router } from '@angular/router';
})
export class LoginComponent implements OnInit {

constructor( private router: Router ) { }
public formSubmitted = false;
public auth2: any;

public loginForm = this.fb.group({
email: [ localStorage.getItem('email') || '' , [ Validators.required, Validators.email ] ],
password: ['', Validators.required ],
remember: [false]
});


constructor( private router: Router,
private fb: FormBuilder,
private usuarioService: UsuarioService,
private ngZone: NgZone ) { }

ngOnInit(): void {
this.renderButton();
}


login() {
this.router.navigateByUrl('/');

this.usuarioService.login( this.loginForm.value )
.subscribe( resp => {

if ( this.loginForm.get('remember').value ){
localStorage.setItem('email', this.loginForm.get('email').value );
} else {
localStorage.removeItem('email');
}

// Navegar al Dashboard
this.router.navigateByUrl('/');

}, (err) => {
// Si sucede un error
Swal.fire('Error', err.error.msg, 'error' );
});

}

renderButton() {
gapi.signin2.render('my-signin2', {
'scope': 'profile email',
'width': 240,
'height': 50,
'longtitle': true,
'theme': 'dark',
});

this.startApp();

}

async startApp() {

await this.usuarioService.googleInit();
this.auth2 = this.usuarioService.auth2;

this.attachSignin( document.getElementById('my-signin2') );

};

attachSignin(element) {

this.auth2.attachClickHandler( element, {},
(googleUser) => {
const id_token = googleUser.getAuthResponse().id_token;
// console.log(id_token);
this.usuarioService.loginGoogle( id_token )
.subscribe( resp => {
// Navegar al Dashboard
this.ngZone.run( () => {
this.router.navigateByUrl('/');
})
});

}, (error) => {
alert(JSON.stringify(error, undefined, 2));
});
}

}
49 changes: 42 additions & 7 deletions src/app/auth/register/register.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,80 @@
<section id="wrapper" class="login-register login-sidebar" style="background-image:url(../assets/images/background/login-register.jpg);">
<div class="login-box card">
<div class="card-body">
<form class="form-horizontal form-material" id="loginform" action="index.html">

<form class="form-horizontal form-material"
id="loginform"
autocomplete="off"
[formGroup]="registerForm"
(ngSubmit)="crearUsuario()">

<a href="javascript:void(0)" class="text-center db"><img src="../assets/images/logo-icon.png" alt="Home" /><br/><img src="../assets/images/logo-text.png" alt="Home" /></a>
<h3 class="box-title m-t-40 m-b-0">Register Now</h3><small>Create your account and enjoy</small>
<div class="form-group m-t-20">
<div class="col-xs-12">
<input class="form-control" type="text" required="" placeholder="Name">
<input class="form-control"
type="text"
placeholder="Name"
formControlName="nombre">
</div>
</div>
<div class="form-group ">
<div class="col-xs-12">
<input class="form-control" type="text" required="" placeholder="Email">
<input class="form-control"
type="text"
placeholder="Email"
formControlName="email">
</div>
</div>
<div class="form-group ">
<div class="col-xs-12">
<input class="form-control" type="password" required="" placeholder="Password">
<input class="form-control"
type="password"
placeholder="Password"
formControlName="password">
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<input class="form-control" type="password" required="" placeholder="Confirm Password">
<input class="form-control"
type="password"
placeholder="Confirm Password"
formControlName="password2">
</div>
</div>

<div class="form-group row">
<div class="col-md-12">
<div class="checkbox checkbox-primary p-t-0">
<input id="checkbox-signup" type="checkbox">

<input id="checkbox-signup"
type="checkbox"
formControlName="terminos">

<label for="checkbox-signup"> I agree to all <a href="#">Terms</a></label>
</div>
</div>
</div>

<div class="row">
<div class="col text-danger">

<p *ngIf="campoNoValido('nombre')">* El nombre es obligatorio</p>
<p *ngIf="campoNoValido('email')">* El email es obligatorio y tiene que ser válido</p>
<p *ngIf="contrasenasNoValidas()">* Las contraseñas deben de ser iguales</p>
<p *ngIf="aceptaTerminos()">* Debe de aceptar los términos de uso</p>

</div>
</div>

<div class="form-group text-center m-t-20">
<div class="col-xs-12">
<button class="btn btn-info btn-lg btn-block text-uppercase waves-effect waves-light" type="submit">Sign Up</button>
</div>
</div>
<div class="form-group m-b-0">
<div class="col-sm-12 text-center">
<p>Already have an account? <a href="pages-login-2.html" class="text-info m-l-5"><b>Sign In</b></a></p>
<p>Already have an account? <a routerLink="/login" class="text-info m-l-5"><b>Sign In</b></a></p>
</div>
</div>
</form>
Expand Down
90 changes: 86 additions & 4 deletions src/app/auth/register/register.component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,97 @@
import { Component, OnInit } from '@angular/core';
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, Validators, FormGroup } from '@angular/forms';
import Swal from 'sweetalert2'

import { UsuarioService } from '../../services/usuario.service';

@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: [ './register.component.css' ]
})
export class RegisterComponent implements OnInit {
export class RegisterComponent {

public formSubmitted = false;

public registerForm = this.fb.group({
nombre: ['Fernando', Validators.required ],
email: ['test100@gmail.com', [ Validators.required, Validators.email ] ],
password: ['123456', Validators.required ],
password2: ['123456', Validators.required ],
terminos: [ true, Validators.required ],
}, {
validators: this.passwordsIguales('password', 'password2')
});

constructor( private fb: FormBuilder,
private usuarioService: UsuarioService,
private router: Router ) { }

crearUsuario() {
this.formSubmitted = true;
console.log( this.registerForm.value );

if ( this.registerForm.invalid ) {
return;
}

// Realizar el posteo
this.usuarioService.crearUsuario( this.registerForm.value )
.subscribe( resp => {

// Navegar al Dashboard
this.router.navigateByUrl('/');

}, (err) => {
// Si sucede un error
Swal.fire('Error', err.error.msg, 'error' );
});


}

campoNoValido( campo: string ): boolean {

if ( this.registerForm.get(campo).invalid && this.formSubmitted ) {
return true;
} else {
return false;
}

}

contrasenasNoValidas() {
const pass1 = this.registerForm.get('password').value;
const pass2 = this.registerForm.get('password2').value;

if ( (pass1 !== pass2) && this.formSubmitted ) {
return true;
} else {
return false;
}

}

aceptaTerminos() {
return !this.registerForm.get('terminos').value && this.formSubmitted;
}

passwordsIguales(pass1Name: string, pass2Name: string ) {

return ( formGroup: FormGroup ) => {

const pass1Control = formGroup.get(pass1Name);
const pass2Control = formGroup.get(pass2Name);

if ( pass1Control.value === pass2Control.value ) {
pass2Control.setErrors(null)
} else {
pass2Control.setErrors({ noEsIgual: true })
}

constructor() { }

ngOnInit(): void {
}
}

}
Loading

0 comments on commit 97a9dc7

Please sign in to comment.