Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 3ad89e7

Browse files
committed
Added updateProfile method
1 parent c94e75e commit 3ad89e7

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

docs/AUTHENTICATION.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,21 @@ Don't forget to enable email-password login in your firebase instance.
166166
);
167167
```
168168

169+
#### Updating a profile
170+
```js
171+
firebase.updateProfile({
172+
displayName: 'Eddy Verbruggen',
173+
photoURL: 'http://provider.com/profiles/eddyverbruggen.png'
174+
}).then(
175+
function () {
176+
// called when update profile was successful
177+
},
178+
function (errorMessage) {
179+
console.log(errorMessage);
180+
}
181+
);
182+
```
183+
169184
#### Resetting a password
170185
```js
171186
firebase.resetPassword({

firebase.android.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,49 @@ firebase.deleteUser = function (arg) {
890890
});
891891
};
892892

893+
firebase.updateProfile = function (arg) {
894+
return new Promise(function (resolve, reject) {
895+
try {
896+
if (!arg.displayName && !arg.photoURL) {
897+
reject("Updating a profile requires a displayName and / or a photoURL argument");
898+
} else {
899+
var firebaseAuth = com.google.firebase.auth.FirebaseAuth.getInstance();
900+
var user = firebaseAuth.getCurrentUser();
901+
902+
if (user === null) {
903+
reject("No current user");
904+
return;
905+
}
906+
907+
var onCompleteListener = new com.google.android.gms.tasks.OnCompleteListener({
908+
onComplete: function (task) {
909+
if (task.isSuccessful()) {
910+
resolve();
911+
} else {
912+
reject("Updating a profile failed. " + (task.getException() && task.getException().getReason ? task.getException().getReason() : task.getException()));
913+
}
914+
}
915+
});
916+
917+
var profileUpdateBuilder = new com.google.firebase.auth.UserProfileChangeRequest.Builder();
918+
919+
if (arg.displayName)
920+
profileUpdateBuilder.setDisplayName(arg.displayName)
921+
922+
if (arg.photoURL)
923+
profileUpdateBuilder.setPhotoUri(android.net.Uri.parse(arg.photoURL))
924+
925+
var profileUpdate = profileUpdateBuilder.build();
926+
927+
user.updateProfile(profileUpdate).addOnCompleteListener(onCompleteListener);
928+
}
929+
} catch (ex) {
930+
console.log("Error in firebase.updateProfile: " + ex);
931+
reject(ex);
932+
}
933+
});
934+
};
935+
893936
firebase.keepInSync = function (path, switchOn) {
894937
return new Promise(function (resolve, reject) {
895938
try {

firebase.ios.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,43 @@ firebase.deleteUser = function (arg) {
10051005
});
10061006
};
10071007

1008+
firebase.updateProfile = function (arg) {
1009+
return new Promise(function (resolve, reject) {
1010+
try {
1011+
var onCompletion = function (error) {
1012+
if (error) {
1013+
reject(error.localizedDescription);
1014+
} else {
1015+
resolve();
1016+
}
1017+
};
1018+
1019+
var fAuth = FIRAuth.auth();
1020+
if (fAuth === null) {
1021+
reject("Run init() first!");
1022+
return;
1023+
}
1024+
1025+
if (!arg.displayName && !arg.photoURL) {
1026+
reject("Updating a profile requires a displayName and / or a photoURL argument");
1027+
} else {
1028+
var user = fAuth.currentUser;
1029+
if (user) {
1030+
var changeRequest = user.profileChangeRequest();
1031+
changeRequest.displayName = arg.displayName;
1032+
changeRequest.photoURL = NSURL.URLWithString(arg.photoURL);
1033+
changeRequest.commitChangesWithCompletion(onCompletion);
1034+
} else {
1035+
reject();
1036+
}
1037+
}
1038+
} catch (ex) {
1039+
console.log("Error in firebase.updateProfile: " + ex);
1040+
reject(ex);
1041+
}
1042+
});
1043+
};
1044+
10081045
firebase._addObservers = function(to, updateCallback) {
10091046
var listeners = [];
10101047
listeners.push(to.observeEventTypeWithBlock(FIRDataEventType.FIRDataEventTypeChildAdded, function (snapshot) {

index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ export interface CreateUserOptions {
236236
password: string;
237237
}
238238

239+
/**
240+
* The options object passed into the updateProfile function.
241+
*/
242+
export interface UpdateProfileOptions {
243+
displayName: string;
244+
photoURL: string;
245+
}
246+
239247
/**
240248
* The options object passed into the resetPassword function.
241249
*/
@@ -522,6 +530,8 @@ export function createUser(options: CreateUserOptions): Promise<CreateUserResult
522530

523531
export function deleteUser(): Promise<any>;
524532

533+
export function updateProfile(options: UpdateProfileOptions): Promise<any>;
534+
525535
export function resetPassword(options: ResetPasswordOptions): Promise<any>;
526536

527537
export function changePassword(options: ChangePasswordOptions): Promise<any>;

0 commit comments

Comments
 (0)