@ -30,16 +30,26 @@ export class AuthService {
userModel : User ;
parrain : Parrain ;
commercant : Commercant ;
utilisateur : any ;
test : string ;
uti : firebase.User ;
profileData : FirebaseListObservable < any [ ] > ;
profileDataParrain : FirebaseListObservable < any [ ] > ;
profileDataCommercant : FirebaseListObservable < any [ ] > ;
boolUpdate : boolean ;
boolSetData : boolean ;
authState : firebase.User ;
@Input ( ) folder : string ;
//fileList: FirebaseListObservable<Image[]>;
constructor ( private http : HttpModule , private afAuth : AngularFireAuth , private afDb : AngularFireDatabase , private router : Router ) {
this . parrain = new Parrain ( ) ;
this . commercant = new Commercant ( ) ;
afAuth . authState . subscribe (
( auth ) = > {
this . authState = auth ;
@ -51,23 +61,23 @@ export class AuthService {
get authenticated ( ) : boolean {
return this . authState !== null ;
}
saveInfo ( user : firebase.User , infoUser : any ) {
saveInfo ( user : firebase.User , infoUser : any ) : boolean {
if ( infoUser . status === 'Parrain' ) {
const userRef = this . databaseRef . child ( 'Parrain' ) . child ( u ser. uid ) ;
const userRef = this . databaseRef . child ( 'Parrain' ) . child ( infoU ser. uid ) ;
// var parrainInfo = infoUser;
infoUser . uid = user . uid ;
// infoUser.uid = user.uid;
userRef . set ( infoUser ) ;
this . upload ( infoUser ) ;
firebase . auth ( ) . signOut ( ) ;
//this.router.navigate(['/carteParrainageComponent']);
return true ;
// this.router.navigate(['/carteParrainageComponent']);
} else if ( infoUser . status === 'Commercant' ) {
const userReff = this . databaseRef . child ( 'Commercant' ) . child ( u ser. uid ) ;
const userReff = this . databaseRef . child ( 'Commercant' ) . child ( infoU ser. uid ) ;
// var commercantInfo = infoUser;
infoUser . uid = user . uid ;
// infoUser.uid = user.uid;
userReff . set ( infoUser ) ;
this . upload ( infoUser ) ;
firebase . auth ( ) . signOut ( ) ;
//this.router.navigate(['/opCommercial']);
return true ;
// this.router.navigate(['/opCommercial']);
} else {
return false ;
}
}
@ -75,28 +85,97 @@ export class AuthService {
signIn ( email : string , password : string ) {
firebase . auth ( ) . signInWithEmailAndPassword ( email , password ) . then ( user = > {
if ( user ) {
if ( /*user.emailVerified*/ true ) {
console . log ( 'salut le ' + user . displayName + ' ' + user . email + ' has signed!' ) ;
if ( user . displayName === 'Parrain' ) {
this . router . navigate ( [ '/carteParrainageComponent' ] ) ;
this . profileDataParrain = this . afDb . list ( '/Parrain/' , {
query : {
orderByChild : 'uid' ,
equalTo : user.uid.toString ( )
}
}
) ;
this . profileDataParrain . subscribe ( snapshot = > {
if ( snapshot . length > 0 ) {
this . profileDataParrain . forEach ( Parrain = > {
Parrain . forEach ( elementParrain = > {
this . parrain = elementParrain ;
this . utilisateur = elementParrain ;
if ( /*user.emailVerified*/ true ) {
console . log ( 'salut le ' + this . utilisateur . email + ' ' + this . utilisateur . status + ' has signed!' ) ;
firebase . auth ( ) . onAuthStateChanged ( function ( userUpdate ) {
userUpdate . updateProfile ( {
displayName : elementParrain.status ,
photoURL : elementParrain.image.downloadURL
} ) ;
} ) ;
if ( this . utilisateur . status === 'Parrain' ) {
this . router . navigate ( [ '/carteParrainageComponent' ] ) ;
} else if ( this . utilisateur . status === 'Commercant' ) {
this . router . navigate ( [ '/opCommercial' ] ) ;
}
} / * else {
console . log ( 'email non verifié!!' ) ;
} * /
} ) ;
} ) ;
}
else if ( user . displayName === 'Commercant' ) {
this . router . navigate ( [ '/opCommercial' ] ) ;
} ) ;
this . profileDataCommercant = this . afDb . list ( '/Commercant/' , {
query : {
orderByChild : 'uid' ,
equalTo : user.uid.toString ( )
}
}
else if ( user . email === 'xjc@gmail.com' ) {
this . router . navigate ( [ '/demandeOpCommercialAdmin' ] ) ;
) ;
this . profileDataCommercant . subscribe ( snapshot = > {
if ( snapshot . length > 0 ) {
this . profileDataCommercant . forEach ( Commercant = > {
Commercant . forEach ( elementCommercant = > {
this . commercant = elementCommercant ;
this . utilisateur = elementCommercant ;
if ( /*user.emailVerified*/ true ) {
console . log ( 'salut le ' + this . utilisateur . email + ' ' + this . utilisateur . status + ' has signed!' ) ;
firebase . auth ( ) . onAuthStateChanged ( function ( userUpdate ) {
userUpdate . updateProfile ( {
displayName : elementCommercant.status ,
photoURL : elementCommercant.image.downloadURL
} ) ;
} ) ;
if ( this . utilisateur . status === 'Parrain' ) {
this . router . navigate ( [ '/carteParrainageComponent' ] ) ;
} else if ( this . utilisateur . status === 'Commercant' ) {
this . router . navigate ( [ '/opCommercial' ] ) ;
}
} / * else {
console . log ( 'email non verifié!!' ) ;
} * /
} ) ;
} ) ;
}
} / * else {
console . log ( 'email non verifié!!' ) ;
} * /
} ) ;
if ( user . email === 'xjc@gmail.com' ) {
this . router . navigate ( [ '/demandeOpCommercialAdmin' ] ) ;
}
/ *
let userTest : any ;
this . profileData = this . afDb . list ( '/' + this . utilisateur + '/' , {
query : {
orderByChild : 'uid' ,
equalTo : user.uid.toString ( )
}
}
) ;
* /
} else {
console . log ( 'error!!' ) ;
}
} ) ;
}
setUserInfo ( user : firebase.User , infoUser : any ) {
/ *
setUserInfo ( user : firebase.User , infoUser : any ) : boolean {
const imagePath = 'profilImage/' + user . uid + '/userPic.jpg' ;
firebase . auth ( ) . onAuthStateChanged ( function ( userUpdate ) {
userUpdate . updateProfile ( {
@ -104,23 +183,35 @@ export class AuthService {
photoURL : imagePath
} ) ;
if ( userUpdate ) {
console . log ( 'userUpdate : ' + userUpdate ) ;
console . log ( 'userUpdate ee : ' + userUpdate ) ;
} else {
console . log ( 'pas user' ) ;
}
} ) ;
this. saveInfo ( user , infoUser ) ;
return true ;
}
* /
signUp ( infoUser : any ) {
firebase . auth ( ) . createUserWithEmailAndPassword ( infoUser . email , infoUser . password ) . then ( userCreate = > {
firebase . auth ( ) . createUserWithEmailAndPassword ( infoUser . email , infoUser . password )
. then ( userCreate = > {
userCreate . sendEmailVerification ( ) . then ( res = > {
console . log ( 'res : ' ) ;
} ) ;
this . setUserInfo ( userCreate , infoUser ) ;
infoUser . uid = userCreate . uid ;
// this.setUserInfo(userCreate, infoUser);
if ( this . saveInfo ( userCreate , infoUser ) ) {
if ( this . upload ( infoUser ) ) {
console . log ( 'youhoo!!!!' ) ;
} else {
console . log ( 'upload failed !!! ' ) ;
}
} else {
console . log ( 'save info failed !!! ' ) ;
}
} ) ;
} )
}
@ -129,7 +220,41 @@ export class AuthService {
}
upload ( info : any ) {
upload ( info : any ) : boolean {
const storageRef = firebase . storage ( ) . ref ( ) ;
let success = false ;
this . folder = '/' + info . status + '/' + info . uid + '/image' ;
for ( const selectedFile of [ ( < HTMLInputElement > document . getElementById ( 'orangeForm-File' ) ) . files [ 0 ] ] ) {
console . log ( selectedFile ) ;
const folder = this . folder ;
const path = ` ${ this . folder } ` ;
const iRef = storageRef . child ( path ) ;
iRef . put ( selectedFile ) . then ( ( snapshot ) = > {
const updates = { } ;
info . image . path = path ;
info . image . filename = 'Profil picture' ;
info . image . downloadURL = snapshot . downloadURL ;
updates [ folder ] = info . image ;
firebase . database ( ) . ref ( ) . update ( updates ) ;
} ) ;
}
firebase . auth ( ) . onAuthStateChanged ( function ( userUpdate ) {
userUpdate . updateProfile ( {
displayName : info.status ,
photoURL : info.image.downloadURL
} ) . then ( al = > {
console . log ( 'userUpdate.dispppp : ' + al ) ;
// firebase.auth().signOut();
} ) ;
} ) ;
return true ;
}
uploadModify ( info : any ) {
const storageRef = firebase . storage ( ) . ref ( ) ;
const success = false ;
this . folder = '/' + info . status + '/' + info . uid + '/image' ;
@ -190,13 +315,13 @@ export class AuthService {
// var parrainInfo = infoUser;
infoUser . uid = user . uid ;
userRef . update ( infoUser ) ;
this . upload ( infoUser ) ;
this . upload Modify ( infoUser ) ;
} else if ( infoUser . status === 'Commercant' ) {
const userReff = this . databaseRef . child ( 'Commercant' ) . child ( user . uid ) ;
// var commercantInfo = infoUser;
infoUser . uid = user . uid ;
userReff . update ( infoUser ) ;
this . upload ( infoUser ) ;
this . upload Modify ( infoUser ) ;
}
}