Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function get_token(data, res, exists) {
//Create a new user.
let uid = data["user_id"] + "@" + data["oauth_consumer_key"];
admin
.auth()
.createCustomToken(uid)
.then(function(customToken) {
console.log("Custom token here: ", customToken);
let message = "user_id -> " + data["user_id"] + "\n";
message += "oauth_consumer_key -> " + data["oauth_consumer_key"] + "\n";
message += "create user -> " + uid + "\n";
message += "redirect to\n\n ";
message += data["APP_REDIRECT_URL"] + customToken;
var link = data["APP_REDIRECT_URL"] + customToken;
var newString = uid.split(".").join("---");
// data['oauth_consumer_key'] = newString;
if (exists) {
admin
photoURL: (user.images && user.images.length > 0 && isValidUrl(user.images[0].url))
? user.images[0].url
: undefined,
email: user.email,
};
let loginUid;
try {
if (linkedAccountUid) {
await admin.auth().updateUser(linkedAccountUid, userMeta);
loginUid = linkedAccountUid;
} else {
// If user does not exist we create it.
const [oldUser, newUser] = await Promise.all([
admin.auth().getUser(ctx.auth.uid),
admin.auth().createUser({
...userMeta,
displayName: user.display_name || user.id,
}),
]);
await admin.auth().setCustomUserClaims(newUser.uid, { spotify: spotifyId });
if (oldUser.providerData.length === 0) {
await transferData(ctx.auth.uid, newUser.uid);
}
loginUid = newUser.uid;
}
} catch (error) {
if (error.code === 'auth/invalid-display-name') {
console.error(error, userMeta.displayName);
const uid = 'firebaseUserId123';
let idToken;
// [START set_custom_user_claims]
// Set admin privilege on the user corresponding to uid.
admin.auth().setCustomUserClaims(uid, {admin: true}).then(() => {
// The new custom claims will propagate to the user's ID token the
// next time a new one is issued.
});
// [END set_custom_user_claims]
// [START verify_custom_claims]
// Verify the ID token first.
admin.auth().verifyIdToken(idToken).then((claims) => {
if (claims.admin === true) {
// Allow access to requested admin resource.
}
});
// [END verify_custom_claims]
// [START read_custom_user_claims]
// Lookup the user associated with the specified uid.
admin.auth().getUser(uid).then((userRecord) => {
// The claims can be accessed on the user record.
console.log(userRecord.customClaims.admin);
});
// [END read_custom_user_claims]
// [START set_custom_user_claims_script]
admin.auth().getUserByEmail('user@admin.example.com').then((user) => {
exports.processSignUp = functions.auth.user().onCreate(user => {
const customClaims = {
'https://hasura.io/jwt/claims': {
'x-hasura-default-role': 'user',
'x-hasura-allowed-roles': ['user'],
'x-hasura-user-id': user.uid,
},
}
// Set custom user claims on this newly created user.
return admin
.auth()
.setCustomUserClaims(user.uid, customClaims)
.then(() => {
return admin
.firestore()
.collection('metadata')
.doc(user.uid)
.set({ refreshTime: new Date().getTime() })
})
.catch(error => {
console.log(error)
})
})
async function getFirebaseUser(lineMid, lineAccessToken) {
// Generate Firebase user's uid based on LINE's mid
const firebaseUid = `line:${lineMid}`;
// LINE's get user profile API endpoint
const getProfileOptions = generateLineApiRequest('https://api.line.me/v1/profile', lineAccessToken);
try {
const response = await admin.auth().getUser(firebaseUid);
// Parse user profile from LINE's get user profile API response
const displayName = response.displayName;
const photoURL = response.pictureUrl;
console.log('Create new Firebase user for LINE user mid = "', lineMid, '"');
// Create a new Firebase user with LINE profile and return it
return admin.auth().createUser({
uid: firebaseUid,
displayName: displayName,
photoURL: photoURL,
});
} catch(error) {
// If user does not exist, fetch LINE profile and create a Firebase new user with it
if (error.code === 'auth/user-not-found') {
return rp(getProfileOptions);
}
}).catch((error) => {
// If user does not exists we create it.
if (error.code === 'auth/user-not-found') {
return admin.auth().createUser({
uid: uid,
displayName: displayName,
photoURL: photoURL,
});
}
throw error;
});
function setUserRoleToAuth({ uid, roles }: { uid: string, roles: string[] }) {
console.log(`[setUserRoleToAuth] Setting roles: ${JSON.stringify({ uid, roles })}`);
return admin.auth().setCustomUserClaims(uid, { tanam: roles });
}
const checkUser = (req, res, next) => {
req.user = anonymousUser;
if (req.query.auth_token !== undefined) {
let idToken = req.query.auth_token;
admin.auth().verifyIdToken(idToken).then(decodedIdToken => {
let authUser = {
id: decodedIdToken.user_id,
name: decodedIdToken.name,
avatar: decodedIdToken.picture
};
req.user = authUser;
next();
}).catch(error => {
next();
});
} else {
next();
};
};
app.post('/register', (req, res) => {
const data = req.body;
return admin.auth().createUser(data)
.then(response => {
admin.auth().
res.json(response);
return;
})
.catch(error => {
res.status(400).json(error);
return;
});
});
const verifyUserTokenId = (token, success, error) => {
admin.auth().verifyIdToken(token)
.then(function (decodedToken) {
var uid = decodedToken.uid;
success(uid)
}).catch(function (err) {
error(err)
})
}