Skip to content

Commit

Permalink
feat: Ongbook#21 - Criação de usuário no firebase authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanPena committed Feb 27, 2019
1 parent 02e8253 commit aa3196b
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 39 deletions.
6 changes: 5 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';

import { AngularFireModule } from '@angular/fire';
import { AngularFireAuthModule } from '@angular/fire/auth';
import { AngularFireDatabaseModule } from '@angular/fire/database';
import { environment } from '../environments/environment';

import { BuscaCnpjService } from './busca-cnpj.service';
import { BuscaLatLngService } from './busca-lat-lng.service';
import { EnviaEmailService } from './envia-email.service';
import { AuthService } from './auth.service';
import { EntidadeService } from './entidade.service';

import { AgmCoreModule } from '@agm/core';
import { AgmJsMarkerClustererModule } from '@agm/js-marker-clusterer';
Expand Down Expand Up @@ -46,9 +49,10 @@ import { ModalEntityProfileComponent } from './maps/modal-entity-profile/modal-e
}),
AgmJsMarkerClustererModule,
AngularFireDatabaseModule,
AngularFireAuthModule,
AngularFireModule.initializeApp(environment.firebase)
],
providers: [BuscaCnpjService, BuscaLatLngService, EnviaEmailService],
providers: [BuscaCnpjService, BuscaLatLngService, EnviaEmailService, AuthService, EntidadeService],
bootstrap: [AppComponent],
entryComponents: [
ModalEntityProfileComponent
Expand Down
15 changes: 15 additions & 0 deletions src/app/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';

import { AuthService } from './auth.service';

describe('AuthService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [AuthService]
});
});

it('should be created', inject([AuthService], (service: AuthService) => {
expect(service).toBeTruthy();
}));
});
33 changes: 33 additions & 0 deletions src/app/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Injectable } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';
import { FormGroup } from '@angular/forms';

import { EntidadeService } from './entidade.service';

@Injectable({
providedIn: 'root'
})
export class AuthService {

constructor(public afAuth: AngularFireAuth, private entidadeService: EntidadeService) { }

public criaUsuarioEntidade(formCadastro: FormGroup) {

let email = formCadastro.controls['responsavel'].value['emailResponsavel'];
let senha = formCadastro.controls['responsavel'].value['senhaOk'];

return this.afAuth.auth.createUserWithEmailAndPassword(email, senha)
.then((res) => {

formCadastro.controls['responsavel'].get('uid').setValue(res.user.uid);

return this.entidadeService.criaEntidade(formCadastro);

}).catch((error) => {

console.log(error);
return new Promise((resolve) => resolve("erro"))
})
}

}
15 changes: 15 additions & 0 deletions src/app/entidade.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';

import { EntidadeService } from './entidade.service';

describe('EntidadeService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [EntidadeService]
});
});

it('should be created', inject([EntidadeService], (service: EntidadeService) => {
expect(service).toBeTruthy();
}));
});
26 changes: 26 additions & 0 deletions src/app/entidade.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@angular/core';
import { FormGroup } from '@angular/forms';

import { AngularFireDatabase } from '@angular/fire/database';

@Injectable({
providedIn: 'root'
})
export class EntidadeService {

constructor(private db: AngularFireDatabase) { }

public criaEntidade(formulario: FormGroup) {

return this.db.list('/entidades').push(formulario.value)
.then((res) => {
return new Promise((resolve) => resolve("sucesso"))
}, error => {
console.log(error)
return new Promise((resolve) => resolve("erro"))
});
};



}
8 changes: 4 additions & 4 deletions src/app/home/home.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ <h5 class="modal-title pull-left">Cadastrar Entidade Social</h5>
<div class="modal-body bg-light" [ngClass]="{'pb-1': registrationSteps == 1}">

<div class="input-group form-group" *ngIf="registrationSteps == 1">
<input type="text" id="cnpj" formControlName="cnpj" name="cnpj" #buscaCnpj class="form-control form-control-lg"
<input type="text" id="cnpj" formControlName="cnpj" name="cnpj" #cnpj class="form-control form-control-lg"
placeholder="CNPJ" aria-describedby="basic-addon2">
<div class="input-group-append">
<button class="btn btn-secondary" type="button" (click)="findCnpj(buscaCnpj.value);"><i class="fas fa-search"></i></button>
<button class="btn btn-secondary" type="button" (click)="buscaCnpj(cnpj.value);"><i class="fas fa-search"></i></button>
</div>
</div>

Expand Down Expand Up @@ -123,7 +123,7 @@ <h6 *ngIf="registrationSteps == 3">Coordenador responsável</h6>
</div>

<div class="form-group" *ngIf="registrationSteps == 3">
<input type="email" class="form-control" placeholder="E-mail" formControlName="email" name="email" id="email">
<input type="email" class="form-control" placeholder="E-mail" formControlName="emailResponsavel" name="emailResponsavel" id="emailResponsavel">
</div>

<div class="form-group" *ngIf="registrationSteps == 3">
Expand Down Expand Up @@ -160,7 +160,7 @@ <h6 *ngIf="registrationSteps == 3">Coordenador responsável</h6>
<button type="button" class="btn btn-sm btn-outline-secondary mr-2" *ngIf="registrationSteps != 1 && registrationSteps != 'step4'"
(click)="backStep();">Voltar</button>
<button type="button" class="btn btn-sm btn-success" *ngIf="registrationSteps == 2" (click)="nextStep();">Avançar</button>
<button type="submit" class="btn btn-sm btn-success" *ngIf="registrationSteps == 3">Cadastrar</button>
<button type="submit" class="btn btn-sm btn-success" *ngIf="registrationSteps == 3" [disabled]="!formCadastro.valid">Cadastrar</button>
<small id="emailHelp" class="form-text text-muted text-right mt-2" style="font-size:10px" *ngIf="registrationSteps != 1 && registrationSteps != 'step4'">Passo
{{ registrationSteps }}/4.</small>
</div>
Expand Down
82 changes: 48 additions & 34 deletions src/app/home/home.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Component, OnInit, TemplateRef, ViewChild, ElementRef } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import { NgForm, FormGroup, FormControl } from '@angular/forms';

import { AngularFireDatabase } from '@angular/fire/database';
import { FormGroup, FormControl, Validators } from '@angular/forms';

import { BuscaCnpjService } from '../busca-cnpj.service';
import { BuscaLatLngService } from '../busca-lat-lng.service';
import { EnviaEmailService } from '../envia-email.service';
import { AuthService } from '../auth.service';

@Component({
selector: 'app-home',
Expand All @@ -30,36 +29,38 @@ export class HomeComponent implements OnInit {
public formCadastro: FormGroup;

constructor(private modalService: BsModalService, private BuscaCnpjService: BuscaCnpjService,
private BuscaLatLngService: BuscaLatLngService, private db: AngularFireDatabase, private EnviaEmailService: EnviaEmailService) {

this.formCadastro = new FormGroup({
cnpj: new FormControl(''),
razaoSocial: new FormControl(''),
atividadePrincipal: new FormControl(''),
areaAtuacao: new FormControl(''),
sigla: new FormControl(''),
nomeFantasia: new FormControl(''),
email: new FormControl(''),
lat: new FormControl(''),
lng: new FormControl(''),
receita: new FormControl(''),
responsavel: new FormGroup({
nome: new FormControl(''),
cpf: new FormControl(''),
email: new FormControl(''),
senha: new FormControl(''),
senhaOk: new FormControl('')
})
});
}

ngOnInit() {}
private BuscaLatLngService: BuscaLatLngService, private EnviaEmailService: EnviaEmailService,
private authService: AuthService) {

this.formCadastro = new FormGroup({
cnpj: new FormControl(''),
razaoSocial: new FormControl(''),
atividadePrincipal: new FormControl(''),
areaAtuacao: new FormControl(''),
sigla: new FormControl(''),
nomeFantasia: new FormControl(''),
email: new FormControl('', [ Validators.required, Validators.email ]),
lat: new FormControl(''),
lng: new FormControl(''),
receita: new FormControl(''),
responsavel: new FormGroup({
uid: new FormControl(''),
nome: new FormControl(''),
cpf: new FormControl(''),
emailResponsavel: new FormControl('', [ Validators.required, Validators.email ]),
senha: new FormControl('', [ Validators.required ]),
senhaOk: new FormControl('', [ Validators.required ])
})
});
}

ngOnInit() { this.registrationSteps == 2 }

public openModalEntityRegister(templateEntityRegister: TemplateRef<any>) {
this.modalRef = this.modalService.show(templateEntityRegister, { backdrop: 'static', keyboard: false });
}

findCnpj(value: any) {
buscaCnpj(value: any) {

this.BuscaCnpjService.getCnpj(value).subscribe((res) => {

Expand All @@ -74,7 +75,7 @@ export class HomeComponent implements OnInit {
this.formCadastro.controls['receita'].setValue(res);

this.BuscaLatLngService.getlatlng(endereco).subscribe(data => {

this.formCadastro.controls['lat'].setValue(data['results'][0].geometry.location.lat);
this.formCadastro.controls['lng'].setValue(data['results'][0].geometry.location.lng);

Expand All @@ -89,7 +90,7 @@ export class HomeComponent implements OnInit {

}, err => {
console.log(err);
});
});

}

Expand All @@ -112,10 +113,23 @@ export class HomeComponent implements OnInit {

onSubmit() {

// gravando no database firebase
this.db.list('/entidades').push(
this.formCadastro.value
);
this.authService.criaUsuarioEntidade(this.formCadastro)
.then((res) => {

if (res == "erro") {
//TODO - disparar alerta bootstrap
console.log("Erro ao cadastrar Entidade!")
}

if (res == "sucesso") {
//TODO - disparar alerta bootstrap
console.log("Entidade cadastrada com sucesso!")
}

}).catch((err) => {
//TODO - disparar alerta bootstrap
console.log(err)
});

this.modalRef.hide();

Expand Down

0 comments on commit aa3196b

Please sign in to comment.