This is a question about securely retrieving a user record from backend, when users login with their Facebook accounts.
loginToFacebook = () => {
FB.login((response) => {
FB.api('/me', {fields: 'id,name,timezone,email,age_range,picture'}, (response) => {
this.props.receiveCurrentUser({
uid: response.id,
email: response.email,
name: response.name,
timezone: response.timezone,
picture: response.picture.data.url,
minAge: response.age_range.min,
})
})
}, {scope: 'email, public_profile'})
}
At this point, I have the user's information. I try to find the user record from backend using uid
only right now.
@user = User.find_by(uid: params[:uid]) if params[:uid]
head :unprocessable_entity unless @user
But this can be easily compromised if some person knows the user_id
of anyone's Facebook account (duh). I thought about storing access_token
in the backend and finding user like below:
@user = User.find_by(access_token: params[:access_token]) if params[:access_token]
head :unprocessable_entity unless @user
The problem is that access_token
expires and the access_token
you get from Facebook login can be out of sync from backend.
What's the right way of doing it?
Thank you for your advice in advance.