From 1341258c3205ae2f6a516ef4063209176b7c1a7e Mon Sep 17 00:00:00 2001 From: Dan Head Date: Fri, 13 Oct 2017 19:47:47 +0100 Subject: [PATCH] Fix issue with login not adding details to user database --- server/routes/approveuid.js | 42 +++++++++++++++++++-------------- server/routes/validate.js | 47 +++++++++++++++++++++---------------- src/components/Login.js | 4 +++- 3 files changed, 55 insertions(+), 38 deletions(-) diff --git a/server/routes/approveuid.js b/server/routes/approveuid.js index 347e5ce..b82d9bb 100644 --- a/server/routes/approveuid.js +++ b/server/routes/approveuid.js @@ -8,24 +8,32 @@ const route = function (db) { }); router.get('/:uid', function(req, res, next) { const uid = req.params.uid; - const userRef = db.ref(`/users/${uid}`); - userRef.set({ - approved: true, - }); - admin.auth().createCustomToken(uid, { - approved: true, - }).then(function(customToken) { - return res.json({ - success: true, - token: customToken, + admin.auth().getUser(uid) + .then(function() { + const userRef = db.ref(`/users/${uid}`); + userRef.set({ + approved: true, + }); + admin.auth().createCustomToken(uid, { + approved: true, + }).then(function(customToken) { + return res.json({ + success: true, + token: customToken, + }); + }).catch(function(err) { + console.error('Error creating custom token', err); + return res.status(500).json({ + success: false, + error: err, + }); + }); + }).catch(function(err) { + return res.json({ + success: false, + error: err, + }); }); - }).catch(function(err) { - console.error('Error creating custom token', err); - return res.status(500).json({ - success: false, - error: err, - }); - }); }); return router; } diff --git a/server/routes/validate.js b/server/routes/validate.js index c5820d4..4122bd4 100644 --- a/server/routes/validate.js +++ b/server/routes/validate.js @@ -24,31 +24,38 @@ const route = function(db) { }); } const valid = password === s.val(); - const userRef = db.ref(`users/${uid}`) - .set({ - isAdmin: false, - isAnonymous: true, - approved: valid, - }); - if (valid) { - admin.auth().createCustomToken(uid, { - approved: true, - }).then(function(customToken) { + admin.auth().getUser(uid).then(function() { + const userRef = db.ref(`users/${uid}`) + .set({ + isAdmin: false, + isAnonymous: true, + approved: valid, + }); + if (valid) { + admin.auth().createCustomToken(uid, { + approved: true, + }).then(function(customToken) { + return res.json({ + valid: true, + token: customToken, + }); + }).catch(function(err) { + console.error('Error creating custom token', err); + return res.status(500).json({ + error: 'Internal server error', + }); + }); + } else { return res.json({ - valid: true, - token: customToken, + valid: false, }); - }).catch(function(err) { - console.error('Error creating custom token', err); - return res.status(500).json({ - error: 'Internal server error', - }); - }); - } else { + } + }).catch(function(err) { return res.json({ valid: false, + error: err, }); - } + }); }); }); diff --git a/src/components/Login.js b/src/components/Login.js index 12c46e7..dc2599b 100644 --- a/src/components/Login.js +++ b/src/components/Login.js @@ -15,7 +15,7 @@ class Login extends React.Component { componentWillUpdate(prev, next) { const authProvider = next.authProvider; if (!this.props.user.uid && next.passwordValid && authProvider) { - const password = this.state.password; + const password = next.password; if (authProvider === 'anonymous') { auth.signInAnonymously().then(res => { this.submitUid(res.uid, password); @@ -48,6 +48,7 @@ class Login extends React.Component { this.form.reset(); axios.post('/api/validate', { password }).then((res) => { if (res.data.valid) { + console.log(password); this.setState({ password, passwordValid: true, @@ -104,6 +105,7 @@ class Login extends React.Component {
this.form = el} onSubmit={(e) => this.submitPassword(e)}> this.password = el} placeholder="Password" /> +
)