Fix issue with login not adding details to user database

This commit is contained in:
2017-10-13 19:47:47 +01:00
parent 681dd80d3b
commit 1341258c32
3 changed files with 55 additions and 38 deletions

View File

@@ -8,24 +8,32 @@ const route = function (db) {
}); });
router.get('/:uid', function(req, res, next) { router.get('/:uid', function(req, res, next) {
const uid = req.params.uid; const uid = req.params.uid;
const userRef = db.ref(`/users/${uid}`); admin.auth().getUser(uid)
userRef.set({ .then(function() {
approved: true, const userRef = db.ref(`/users/${uid}`);
}); userRef.set({
admin.auth().createCustomToken(uid, { approved: true,
approved: true, });
}).then(function(customToken) { admin.auth().createCustomToken(uid, {
return res.json({ approved: true,
success: true, }).then(function(customToken) {
token: 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; return router;
} }

View File

@@ -24,31 +24,38 @@ const route = function(db) {
}); });
} }
const valid = password === s.val(); const valid = password === s.val();
const userRef = db.ref(`users/${uid}`) admin.auth().getUser(uid).then(function() {
.set({ const userRef = db.ref(`users/${uid}`)
isAdmin: false, .set({
isAnonymous: true, isAdmin: false,
approved: valid, isAnonymous: true,
}); approved: valid,
if (valid) { });
admin.auth().createCustomToken(uid, { if (valid) {
approved: true, admin.auth().createCustomToken(uid, {
}).then(function(customToken) { 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({ return res.json({
valid: true, valid: false,
token: customToken,
}); });
}).catch(function(err) { }
console.error('Error creating custom token', err); }).catch(function(err) {
return res.status(500).json({
error: 'Internal server error',
});
});
} else {
return res.json({ return res.json({
valid: false, valid: false,
error: err,
}); });
} });
}); });
}); });

View File

@@ -15,7 +15,7 @@ class Login extends React.Component {
componentWillUpdate(prev, next) { componentWillUpdate(prev, next) {
const authProvider = next.authProvider; const authProvider = next.authProvider;
if (!this.props.user.uid && next.passwordValid && authProvider) { if (!this.props.user.uid && next.passwordValid && authProvider) {
const password = this.state.password; const password = next.password;
if (authProvider === 'anonymous') { if (authProvider === 'anonymous') {
auth.signInAnonymously().then(res => { auth.signInAnonymously().then(res => {
this.submitUid(res.uid, password); this.submitUid(res.uid, password);
@@ -48,6 +48,7 @@ class Login extends React.Component {
this.form.reset(); this.form.reset();
axios.post('/api/validate', { password }).then((res) => { axios.post('/api/validate', { password }).then((res) => {
if (res.data.valid) { if (res.data.valid) {
console.log(password);
this.setState({ this.setState({
password, password,
passwordValid: true, passwordValid: true,
@@ -104,6 +105,7 @@ class Login extends React.Component {
<form ref={(el) => this.form = el} onSubmit={(e) => this.submitPassword(e)}> <form ref={(el) => this.form = el} onSubmit={(e) => this.submitPassword(e)}>
<Input type="text" innerRef={(el) => this.password = el} placeholder="Password" /> <Input type="text" innerRef={(el) => this.password = el} placeholder="Password" />
<Button type="submit">Go!</Button> <Button type="submit">Go!</Button>
<Button onClick={() => this.setState({ authProvider: null })}>Back</Button>
</form> </form>
</div> </div>
) )