attach discord id to player model and update on log in

This commit is contained in:
Sendou
2020-04-19 18:57:29 +03:00
parent f12334eb7c
commit 3b87a1e8bc
7 changed files with 75 additions and 43 deletions

View File

@@ -14,9 +14,9 @@
}
},
"@apollo/react-hooks": {
"version": "3.1.4",
"resolved": "https://registry.npmjs.org/@apollo/react-hooks/-/react-hooks-3.1.4.tgz",
"integrity": "sha512-yamD5a1Gu9fGQjZYEQn2nSG+BRyde4dy055agtYOvP/5/njJBSJ25tRFbjxKf7+YW9fsu2Xguib3anoQTuesNg==",
"version": "3.1.5",
"resolved": "https://registry.npmjs.org/@apollo/react-hooks/-/react-hooks-3.1.5.tgz",
"integrity": "sha512-y0CJ393DLxIIkksRup4nt+vSjxalbZBXnnXxYbviq/woj+zKa431zy0yT4LqyRKpFy9ahMIwxBnBwfwIoupqLQ==",
"requires": {
"@apollo/react-common": "^3.1.4",
"@wry/equality": "^0.1.9",
@@ -2153,9 +2153,9 @@
}
},
"@testing-library/user-event": {
"version": "10.0.1",
"resolved": "https://registry.npmjs.org/@testing-library/user-event/-/user-event-10.0.1.tgz",
"integrity": "sha512-M63ftowo1QpAGMnWyz7df0ygqnu4XyF68Sty7mivMAz2HLcY1uLoN3qcen6WMobdY0MoZUi4+BLsziSDAP62Vg=="
"version": "10.0.2",
"resolved": "https://registry.npmjs.org/@testing-library/user-event/-/user-event-10.0.2.tgz",
"integrity": "sha512-fVeP4U37BIYdp9nBRKEITFSLPqgCSS7Og6LHvxoQ2JSOTJ1NJI4Dfesv4uNXxvNNcJgBS88V+Tc6h8vbDsa2iA=="
},
"@types/babel__core": {
"version": "7.1.6",
@@ -12778,9 +12778,9 @@
}
},
"react-draggable": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/react-draggable/-/react-draggable-4.3.0.tgz",
"integrity": "sha512-er6mpn/F/8qLdhGJ21XSzcQl7hTWGsb/af9NaOhSH1M/aivhwf8WHfopmRAF7iRL3x7guLC5TF9HJaouIGHVvw==",
"version": "4.3.1",
"resolved": "https://registry.npmjs.org/react-draggable/-/react-draggable-4.3.1.tgz",
"integrity": "sha512-m8QeV+eIi7LhD5mXoLqDzLbokc6Ncwa0T34fF6uJzWSs4vc4fdZI/XGqHYoEn91T8S6qO+BSXslONh7Jz9VPQQ==",
"requires": {
"classnames": "^2.2.5",
"prop-types": "^15.6.0"
@@ -12792,9 +12792,9 @@
"integrity": "sha512-TAv1KJFh3RhqxNvhzxj6LeT5NWklP6rDr2a0jaTfsZ5wSZWHOGeqQyejUp3xxLfPt2UpyJEcVQB/zyPcmonNFA=="
},
"react-fast-compare": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/react-fast-compare/-/react-fast-compare-2.0.4.tgz",
"integrity": "sha512-suNP+J1VU1MWFKcyt7RtjiSWUjvidmQSlqu+eHslq+342xCbGTYmC0mEhPCOHxlW0CywylOC1u2DFAT+bv4dBw=="
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/react-fast-compare/-/react-fast-compare-3.0.1.tgz",
"integrity": "sha512-C5vP0J644ofZGd54P8++O7AvrqMEbrGf8Ue0eAUJLJyw168dAX2aiYyX/zcY/eSNwO0IDjsKUaLE6n83D+TnEg=="
},
"react-focus-lock": {
"version": "2.2.1",
@@ -12810,21 +12810,36 @@
}
},
"react-helmet-async": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/react-helmet-async/-/react-helmet-async-1.0.4.tgz",
"integrity": "sha512-KTGHE9sz8N7+fCkZ2a3vzXH9eIkiTNhL2NhKR7XzzQl3WsGlCHh76arauJUIiGdfhjeMp7DY7PkASAmYFXeJYg==",
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/react-helmet-async/-/react-helmet-async-1.0.5.tgz",
"integrity": "sha512-nqGA5a1HRZsw1lzDn+bYuUN2FyHRiY+DgjRVhEOKVBDTrrqJCpCIOuY/IRHdobr+KD1gGTP0WabZsTrIHnFKJA==",
"requires": {
"@babel/runtime": "^7.3.4",
"@babel/runtime": "^7.9.2",
"invariant": "^2.2.4",
"prop-types": "^15.7.2",
"react-fast-compare": "^2.0.4",
"react-fast-compare": "^3.0.1",
"shallowequal": "^1.1.0"
},
"dependencies": {
"@babel/runtime": {
"version": "7.9.2",
"resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.9.2.tgz",
"integrity": "sha512-NE2DtOdufG7R5vnfQUTehdTfNycfUANEtCa9PssN9O/xmTzP4E08UI797ixaei6hBEVL9BI/PsdJS5x7mWoB9Q==",
"requires": {
"regenerator-runtime": "^0.13.4"
}
},
"regenerator-runtime": {
"version": "0.13.5",
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.5.tgz",
"integrity": "sha512-ZS5w8CpKFinUzOwW3c83oPeVXoNsrLsaCoLtJvAClH135j/R77RuymhiSErhm2lKcwSCIpmvIWSbDkIfAqKQlA=="
}
}
},
"react-hotkeys-hook": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/react-hotkeys-hook/-/react-hotkeys-hook-2.1.1.tgz",
"integrity": "sha512-51B27x3XtnICEEhi23EweYz5DL1CGNOSY4h4H5lu4ZHdNqvf9Br9KFugCSH2XvjhORCzfIv8/zEnv0BiT/mb3w==",
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/react-hotkeys-hook/-/react-hotkeys-hook-2.1.3.tgz",
"integrity": "sha512-p4cH9n4c8Ac3kkNIf6a/97sLEJq+nnhP7fpT0bHhTwByZhBDU3j5hluu00FomxE3hMzJWk4knYG5ZuUM/qVJPw==",
"requires": {
"hotkeys-js": "3.7.6"
}

View File

@@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@apollo/react-hooks": "^3.1.4",
"@apollo/react-hooks": "^3.1.5",
"@chakra-ui/core": "^0.7.0",
"@emotion/core": "^10.0.28",
"@emotion/styled": "^10.0.27",
@@ -12,7 +12,7 @@
"@sendou/react-sketch": "^0.5.2",
"@testing-library/jest-dom": "^5.5.0",
"@testing-library/react": "^10.0.2",
"@testing-library/user-event": "^10.0.1",
"@testing-library/user-event": "^10.0.2",
"@types/jest": "^25.2.1",
"@types/reach__router": "^1.3.4",
"@types/react": "^16.9.34",
@@ -33,9 +33,9 @@
"react-color": "^2.18.0",
"react-datepicker": "^2.14.1",
"react-dom": "^16.13.1",
"react-draggable": "^4.3.0",
"react-helmet-async": "^1.0.4",
"react-hotkeys-hook": "^2.1.1",
"react-draggable": "^4.3.1",
"react-helmet-async": "^1.0.5",
"react-hotkeys-hook": "^2.1.3",
"react-icons": "^3.9.0",
"react-infinite-scroller": "^1.2.4",
"react-markdown": "^4.3.1",

View File

@@ -31,6 +31,7 @@ export const SEARCH_FOR_PLACEMENTS: DocumentNode = gql`
mode
player {
twitter
discord_id
}
}
pageCount

View File

@@ -78,7 +78,7 @@ const resolvers = {
.sort({ topTotalScore: "desc" })
.limit(args.amount)
.populate("topTotal", { unique_id: 0 })
.catch(e => {
.catch((e) => {
throw new UserInputError(e.message, {
invalidArgs: args,
})
@@ -245,7 +245,7 @@ const resolvers = {
const player = await Player.findOne({
unique_id: args.unique_id.trim(),
}).catch(e => {
}).catch((e) => {
throw new UserInputError(e.message, {
invalidArgs: args,
})
@@ -266,7 +266,7 @@ const resolvers = {
const user = await User.findOne({
twitter_name,
}).catch(e => {
}).catch((e) => {
throw new UserInputError(e.message, {
invalidArgs: args,
})
@@ -287,19 +287,27 @@ const resolvers = {
},
},
Player: {
discord_id: async root => {
discord_id: async (root) => {
if (!root.twitter) return null
if (root.discord_id) return root.discord_id
const user = await User.findOne({ twitter_name: root.twitter }).catch(
e => {
throw (new UserInputError(),
{
invalidArgs: args,
})
(e) => {
throw (
(new UserInputError(),
{
invalidArgs: args,
})
)
}
)
if (!user) return null
await Player.updateOne(
{ twitter: root.twitter },
{ $set: { discord_id: user.discord_id } }
)
return user.discord_id
},
},

View File

@@ -9,6 +9,7 @@ const cors = require("cors")
const passport = require("passport")
const DiscordStrategy = require("passport-discord").Strategy
const User = require("./mongoose-models/user")
const Player = require("./mongoose-models/player")
const path = require("path")
const schema = require("./schema")
@@ -28,7 +29,7 @@ passport.use(
callbackURL,
scope: ["identify", "connections"],
},
function(accessToken, refreshToken, profile, cb) {
async function (accessToken, refreshToken, profile, cb) {
const userToSave = {
username: profile.username,
discriminator: profile.discriminator,
@@ -45,11 +46,16 @@ passport.use(
}
}
User.updateOne(
await Player.updateOne(
{ discord_id: userToSave.discord_id },
{ $set: { twitter: userToSave.twitter_name } }
)
await User.updateOne(
{ discord_id: userToSave.discord_id },
userToSave,
{ upsert: true },
function(err, user) {
function (err, user) {
return cb(err, userToSave)
}
)
@@ -57,12 +63,12 @@ passport.use(
)
)
passport.serializeUser(function(user, done) {
passport.serializeUser(function (user, done) {
done(null, user.discord_id)
})
passport.deserializeUser(function(discord_id, done) {
User.findOne({ discord_id }, function(err, user) {
passport.deserializeUser(function (discord_id, done) {
User.findOne({ discord_id }, function (err, user) {
done(err, user)
})
})
@@ -81,7 +87,7 @@ mongoose
.then(() => {
console.log(`connected to MongoDB (${dbName})`)
})
.catch(error => {
.catch((error) => {
console.log("error connection to MongoDB:", error.message)
})
@@ -181,12 +187,12 @@ app.get(
passport.authenticate("discord", {
failureRedirect: "/404",
}),
function(req, res) {
function (req, res) {
res.redirect("/u/" + req.user.discord_id) // Successful auth
}
)
app.get("/logout", function(req, res) {
app.get("/logout", function (req, res) {
req.logout()
res.redirect("/")
})

View File

@@ -3,6 +3,7 @@ const mongoose = require("mongoose")
const playerSchema = new mongoose.Schema({
name: { type: String, required: true },
unique_id: { type: String, unique: true, required: true },
discord_id: String,
alias: String,
twitter: String,
weapons: [String],

View File

@@ -5,6 +5,7 @@
"main": "index.js",
"scripts": {
"start": "node index.js",
"startprod": "cross-env USE_PRODUCTION_DB=TRUE node index.js",
"withprod": "cross-env LOGGED_IN=TRUE USE_PRODUCTION_DB=TRUE node index.js",
"withdev": "cross-env LOGGED_IN=TRUE node index.js",
"watchdev": "cross-env LOGGED_IN=TRUE nodemon index.js",