Skip to content

Instantly share code, notes, and snippets.

@ginpei
Last active August 18, 2017 22:14
Show Gist options
  • Save ginpei/bcaffd19f80bc9c14f9088e765b61ec7 to your computer and use it in GitHub Desktop.
Save ginpei/bcaffd19f80bc9c14f9088e765b61ec7 to your computer and use it in GitHub Desktop.

Initialize

const firebase = require('firebase')
const admin = require('firebase-admin')

const serviceAccount = require('./serviceAccountKey.json')
const secret = {
	"PROJECT_ID": "xxx",
	"API_KEY": "xxx"
}

firebase.initializeApp({
	apiKey: `${secret.API_KEY}`,
	authDomain: `${secret.PROJECT_ID}.firebaseapp.com`,
	databaseURL: `https://${secret.PROJECT_ID}.firebaseio.com`,
	storageBucket: `${secret.PROJECT_ID}.appspot.com`,
})

admin.initializeApp({
	credential: admin.credential.cert(serviceAccount),
	databaseURL: `https://${secret.PROJECT_ID}.firebaseio.com`,
})

Then

const auth = firebase.auth()

Sign in

auth.signInAnonymously()
	.then(_ => console.log('Signed in. ID:', auth.currentUser.uid))
const uid = user.uid
console.log('User ID:', uid)
user.getIdToken()
	.then(token => console.log('Got a token:', token))

Sign out

auth.signOut()
	.then(token => console.log('Signed out'))

[WIP] Retrieve signing in

if (!auth.currentUser) {
	console.log('Not signed in!')
}

signInWithCustomToken

auth.signInWithCustomToken(token)
	.catch(error => console.log(error.code, error.message))
auth/invalid-custom-token The custom token format is incorrect. Please check the documentation.

signInAndRetrieveDataWithCredential

auth.signInAndRetrieveDataWithCredential(token)
	.catch(error => console.log(error.code, error.message))
auth/argument-error signInAndRetrieveDataWithCredential failed: First argument "credential" must be a valid credential.

signInWithCredential

auth.signInWithCredential(token)
	.catch(error => console.log(error.code, error.message))
auth/argument-error signInWithCredential failed: First argument "credential" must be a valid credential.

verifyIdToken (from Admin)

admin.auth().verifyIdToken(token)
	.then(data => {
		console.log(data)
		console.log('Signed in?', auth.currentUser !== null)
	})

You would get data including uid. Then what's next?

I'm creating a React Native app which provides signing in by Firebase auth and host data by Firebase DB. Signing in itself is working but when you kill the app once, you need to sign in again unlike the other environments, Java for Android or Swift for iOS.

auth.setPersistence(firebase.auth.Auth.Persistence.LOCAL) doesn't work as it says "The current environment does not support the specified persistence type".

Is there a way to keep or retrieve signing in? Or, usually, do you keep only user ID to manage data and ask users to sign in again as need?

To sign in:

const auth = firebase.auth()
auth.signInAnonymously()
  .then(_ => console.log('Signed in. ID:', auth.currentUser.uid))

Now you have auth.currentUser. You can get user ID and token by:

const user = auth.currentUser
const uid = user.uid
console.log('User ID:', uid)

user.getIdToken()
  .then(token => console.log('Got a token:', token))

I tried verifyIdToken() to retrieve auth.currentUser but it seems for another purpose.

admin.auth().verifyIdToken(token)
  .then(data => {
    console.log(data)
    console.log('Signed in?', auth.currentUser !== null)  // => false
  })

And followings:

auth.signInWithCustomToken(token)
  .catch(error => console.log('ERROR:', error.code, error.message))
  // ERROR: auth/invalid-custom-token The custom token format is incorrect. Please check the documentation.

auth.signInAndRetrieveDataWithCredential(token)
  .catch(error => console.log('ERROR:', error.code, error.message))
  // ERROR: auth/argument-error signInAndRetrieveDataWithCredential failed: First argument "credential" must be a valid credential.

auth.signInWithCredential(token)
  .catch(error => console.log('ERROR:', error.code, error.message))
  // ERROR: auth/argument-error signInWithCredential failed: First argument "credential" must be a valid credential.

Since that token is not a custom token nor credential, these results make sense.

I also tried to just get an user, but the result seems to be anything else.

admin.auth().getUser(user.uid)
  .then(data => {
    console.log(data)
    console.log('Does the user have a method to delete?', 'delete' in user)
  })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment