AngularFire simple login - javascript

I have a FireBase db with a users store. I also use simple login email/pw. In the User store I save some extra info of a user - e.g. the lastlogin date. This is my workflow - from registering to logging in:
I register a user;
when registered it is added to the simple login email/pw sote;
I also add the registered user (includng the id returned from the simplelogin) in the users store. It is stored under a Firebase generated unique key.
I log in as that new user
When successful I get a user object from the simplelogin store:
email-"testuser1#test.com"
firebaseAuthToken-"eyJ0eXAiOiJKV1QiLCJhbGci...SoXkddR3A88vAkENCy5ilIk"
id-"46"
isTemporaryPassword-false
md5_hash-"6a4b6cb2045fd55f706eaebd6ab5d4f7"
provider-"password"
uid-"simplelogin:46"
Now I want to update the corresponding user in the User store - e.g. set the lastlogin key to now. But I only can update that user when I know the Firebase generated key it's under. How can I access that key?
The only other way to identify the user in the Users store is by retrieving all users in the Users store, looping through all of them and checking : does the current id key value match the id key value of the logged-in user. Looks a bit clumsy to me but I fear this is the only way I can do lookups with firebase?

When you save a registered user you should save them by their uid rather than a generated id. This way when the user logs back in we'll user the uid to get the user from the users node.
var fbRef = new Firebase('https://<YOUR-FIREBASE>.firebaseio.com');
var auth = new FirebaseSimpleLogin(fbRef, function(error, user) {
if (error) {
console.error(error);
} else if (user) {
// when a user logs in we can update their lastLogin here
// set the key to the uid for the user
// this would look like: https://myapp.firebaseio.com/users/1
fbRef.child('users').child(user.uid).update({
lastLogin: Firebase.ServerValue.TIMESTAMP // the time they logged in
});
}
});
// here when we create a user we will set the key to the uid under the users node
auth.createUser(email, password, function(error, user) {
// if there is no error
if (!error) {
// go to the users node, then set a location at the user's uid
// this would look like: https://myapp.firebaseio.com/users/1
fbRef.child('users').child(user.uid).set(user);
}
});
As the users are created our users node will look like this:

Related

How to add new members in Group using Firebase

I am developing the app which has grouping function.
Now I have the problem about adding new member in group.
Like Slack, in group creating onboarding flow user can decide group name and add members which don't use the app yet.
As adding members function, I want to use inviting mail link using firebase dynamic links.
Data structure is below
User
- id
- name
- email
Group
- id
- groupName
- members[]
Group's members array has user id.
But when someone creates new group, it is possible that new users don't register the app yet.
So they don't have user id property in the app.
How do I fix this problem?
When someone creates new group, should I develop sign up functions new user using Firebase auth? This means then new member has automatically user id, and adding those to members property.
Or should group member array have mail address instead of user id.
Please tell me. I'm happy with Swift or JavaScript you will teach me.
Thank you.
UPDATE
After reading your comment I would propose another approach.
When the Group creator user adds users to a group, if a user does not already exists you could, from the front-end, call a Callable Cloud Function (CF) that creates a temporary Firestore document in a specific collection. The ID of this document will be the (future) userId.
Then, still in this same Cloud Function, you send an email to the email address (you need to generate yourself the email, for example with the email extension) with a link containing the userId as query string value.
Example of code for this first CF:
exports.provisionNewAccount = functions
.https.onCall(async (data, context) => {
try {
// You can check that the caller is authenticated
// if (context.auth.uid) {execute the rest of the code} else {throw an error}
// New user email
const userEmail = data.email;
// Generate the new user docID
const fakeDocRef = admin.firestore().collection('_').doc();
const requestId = fakeDocRef.id;
// Create the doc in a specific collection
await admin.firestore().collection('usersCreationRequests').doc(requestId).set({ email: userEmail, treated: false });
// Generate the link to include in the email
const linkURL = 'https://your.app/register.html?requestId=' + requestId
// Send the email by creating a doc in the Extension collection
await db
.collection("emails")
.add({
to: userEmail,
message: {
subject: "....",
html: `Click to create your account` // adapt the html to add some text
},
});
return {result: 'OK'}
} catch (error) {
console.log(JSON.stringify(error));
throw new functions.https.HttpsError('internal', JSON.stringify(error));
}
});
You call it as explained here, by passing the future user's email.
When the email recipient clicks on the link, you open a specific page of your web app that shows a set of fields for the future user to enter his password, display name etc. Then on clicking on a sign-in button in this page you call a Callable Cloud Function passing it the Firestore document ID plus the field values (you get the document ID from the query string).
As shown below, this Cloud Function creates the user in the Authentication service (using the Admin SDK) and flag the Firestore document as treated. Upon getting back the Cloud Function result in the web app you authenticate the user (you have his email and password, since he/she entered it in the form).
exports.createNewAccount = functions
.https.onCall(async (data, context) => {
try {
const userEmail = data.email;
const userId = data.userId;
const userPassword = data.password;
const userDisplayName = data.displayName;
// Fetch the user doc created in the first CF
const snapshot = await admin.firestore().collection('usersCreationRequests').doc(userId).get();
const treated = snapshot.get('treated');
const email = snapshot.get('email');
if (!treated && userEmail === email) {
const createUserPayload = {
email,
emailVerified: false,
password: userPassword,
displayName: userDisplayName
};
const userRecord = await admin.auth().createUser(createUserPayload);
return { result: 'OK' }
} else {
return { result: 'User already created' }
}
} catch (error) {
console.log(JSON.stringify(error));
throw new functions.https.HttpsError('internal', JSON.stringify(error));
}
});
I’m actually using this exact approach for a B2B collaborative web app in which users can invite new users by email.
INITIAL ANSWER
(Totally different from the update)
So they don't have user id property in the app… How do I fix this
problem? When someone creates new group, should I develop sign up
functions new user using Firebase auth?
You can use the Anonymous Authentication mode, it exactly corresponds to your needs:
You can use Firebase Authentication to create and use temporary
anonymous accounts to authenticate with Firebase. These temporary
anonymous accounts can be used to allow users who haven't yet signed
up to your app to work with data protected by security rules. If an
anonymous user decides to sign up to your app, you can link their
sign-in credentials to the anonymous account so that they can continue
to work with their protected data in future sessions.
When signing-in with Anonymous Authentication a userId (uid) will be created and later you will be able to convert an anonymous account to a permanent account
I always use userId to achive this kind of feature, you can use anonymous authentication to get userId after user click invite link, Then if needed unlock more feature with furter authentication(add more provider).
If you only using mail address without authentication, It's hard to write rules for prevent user access unwanted data, Like anyone knew your email are able to access your account.

How to check if user is logged in without request.session

I am implementing a web application using Express and Docker. I am also using a Three layered architecture. When a user logs in, I store that information in a session. I have blogposts as a resource in my app. To retrieve the blogpostId I will send a query to the database in the Data access layer like this:
const db = require('./db')
exports.getBlogpostId = function(id ,callback){
const query = "SELECT * FROM blogposts WHERE blogId = ?"
const value = [id]
db.query(query, value, function(error, blogpost){
if(error){
callback("DatabaseError", null)
}else{
callback(null, blogpost)
}
})
}
Now in my Business logic layer I want to check if the user is logged in or not, something like this:
const blogRepo = require('../dal/blog-repository')
exports.getBlogpostId = function(id){
if(/*If the user is logged in*/){
return blogRepo.getBlogpostId(id)
}else{
throw "Unauthorized!"
}
}
How can I check if they are logged in here. How can I get the session that I stored when they logged in?
Thanks!
So, the business layer doesn't generically know anything about the logged in state at all. It's business logic, not web logic.
If you want it to have access to state like that, you have to pass that state into it as arguments any time you call it.
You can either decide that it's OK for the business logic to see the session object and pass the whole session object into it or you need to pass the specific pieces of the session object that the business logic needs such as the authentication state.

Firebase Realtime Database Show Users Name - Web Page

I have to show data belonging to a user that logged in.
My database structure is as follows:
Users/Drivers/Uid/name,phone...
How can I show the data of a user that logged in by using JavaScript?
If you have a structure like that, you can display all data for the currently logged in user with:
var currentUser = firebase.auth().currentUser;
var userRef = firebase.database().ref("Users/Drivers").child(currentUser.uid);
userRef.on("value").then(function(snapshot) {
console.log(snapshot.val());
})

#ionic change the data key in firebase

I'm trying to update/add data on firebase. I used the Facebook login and I want to use the UserID as a key for the new data aded.
(check pict below)
The userID that I want to use it:
I want to replace that key with the userID:
fblogin(){
this.facebook.login(['email'])
.then(res=> {
const fc = firebase.auth.FacebookAuthProvider.credential(res.authResponse.accessToken);
firebase.auth().signInWithCredential(fc)
.then(fs => {
this.facebook.api('me?fields=id,name,email,first_name,picture.width(720).height(720).as(picture_large)', []).then(profile => {
this.newuser = {name: profile['first_name'] ,email: profile['email'],picture: profile['picture_large']['data']['url'],phone:''}
this.navCtrl.push(HomePage);
console.log(fs.uid);
this.db.list('/users/'+ fs.uid).update(this.newuser);
});
I got this error in compilation:
supplied parameters do not matchany signature of call target
In this line: this.db.list('/users/'+ fs.uid).update(this.newuser);
Any help?
The FB error looks correct. You cant update on the uid as the user has been saved with a unique FB id
You do not show the code that created the users record in the database, but what i think you want to do is set and object when you first save the users record. However this could be an issue because the user could be saved before the return of the uid. I cant tell with your code snippet. Regardless, I will write the code that i think will work if the users/ record is created at the time that of registration.
The service
async signupUser(email: string, password: string) {
try {
const result = await this.afA.auth.createUserWithEmailAndPassword(email, password);
return result;
} catch (err) {
console.log('error', err);
}
}
So this initially creates a user without facebook, The key here is that the users FB uid was created and is held in the returned result
The component
this.authData.signupUser(email,password).then(userData => {
console.log(userData) // <- this is result
}
Then we create a record in FB with the uid returned
this.db.object(`users/${userData.uid}/`).set(data);
.set(data) is whatever data you want to save in the users/uid namespace.
So basically you need to create that user table with its uid namespace when the user first registers. Then you can update the user with the uid returned from the facebook fs.uid
With your current code you could find the user based on the email ( because the email should be unique to all users) and then update ...
with lodash is it just
let foundUser = find(this.db.list('users'),{ 'email' : fs.email }
// and then update based on the object key
this.db.list('/users/'+ Object.keys(foundUser)).update(this.newuser);
i fixed the problem by using:
this.db.object('/users/'+ fs.uid).update(this.newuser);
instead of :
this.db.list('/users/'+ fs.uid).update(this.newuser);
And it works correctly !
Thanks all for help.

Firebase - Check if logged in user id equals realtime database value

currently I'm working on a new Firebase web project.
I want to check if the current logged in user ID is saved in the realtime database to give the user some specific possibilities / rights on my site.
Realtime Database:
users
>*User ID* (the value to compare)
My question: Which JavaScript code can compare the currently logged in Firebase user with this db value?
I'm sure by now you have found that all your questions have been answered on the firebase documentation.
On your web app, the currently signed in user can be gotten using
firebase.auth().currentUser;
To compare the logged in user with child key do this
var user_id = firebase.auth().currentUser.uid;
firebase.database().ref('users/'+user_id).once('value').then(userSnapshot => {
if(userSnapshot.exists()){
//allow user perform action
}else{
// do not allow
}
}).catch(error => {
console.error(error);
});

Categories

Resources