fix: login not closing
This commit is contained in:
1 parent
744c6af92c
commit
2caf7d2e9b
3 files changed
+40
-26
No files matched your search
@@ -217,10 +217,11 @@ Exchange Facebook login code for an access token
|
||||
|
||||
Enviroment variables required
|
||||
|
||||
### Ports
|
||||
### Ports and domains
|
||||
|
||||
```ini
|
||||
HTTP_PORT=3010
|
||||
HTTP_DOMAIN=radio.rolniknysa.pl
|
||||
WS_PORT=3020
|
||||
```
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ const hostName = process.env.CACHE_HOST
|
||||
const client = redis.createClient({ host: hostName, port })
|
||||
|
||||
client.on('ready', function (error) {
|
||||
console.error(`cache: connected`)
|
||||
console.info(`cache: connected`)
|
||||
})
|
||||
|
||||
const cache = {
|
||||
|
||||
+37
-24
@@ -9,7 +9,8 @@ dotenv.config()
|
||||
const router = express.Router()
|
||||
|
||||
const appId = process.env.FB_APPID,
|
||||
secret = process.env.FB_SECRET
|
||||
secret = process.env.FB_SECRET,
|
||||
domain = process.env.HTTP_DOMAIN
|
||||
|
||||
router.get('/token', ({ query: { code } }, res) => {
|
||||
if (code === undefined) return res.status(400).end('Facebook code required')
|
||||
@@ -17,32 +18,18 @@ router.get('/token', ({ query: { code } }, res) => {
|
||||
return res.status(400).end('Facebook code needs to be of non zero length')
|
||||
|
||||
got(
|
||||
`https://graph.facebook.com/v12.0/oauth/access_token?client_id=${appId}&redirect_uri=${'https://radio.rolniknysa.pl/api/login/token'}&client_secret=${secret}&code=${code}`
|
||||
`https://graph.facebook.com/v12.0/oauth/access_token?client_id=${appId}&redirect_uri=https://${domain}/api/login/token&client_secret=${secret}&code=${code}`
|
||||
)
|
||||
.json()
|
||||
.then(({ access_token, expires_in }) => {
|
||||
got(`https://graph.facebook.com/me?access_token=${access_token}`)
|
||||
.json()
|
||||
.then(({ name, id }) => {
|
||||
const jwt = Jwt.sign(
|
||||
{
|
||||
name,
|
||||
id,
|
||||
},
|
||||
secret
|
||||
)
|
||||
|
||||
res.end(
|
||||
`<body>Logowanie...</body><script>window.onload=function(){opener.exit(${JSON.stringify(
|
||||
{ jwt }
|
||||
)})}</script>`
|
||||
)
|
||||
|
||||
db.users
|
||||
.getUser(id)
|
||||
.then((user) => {
|
||||
if (user === null)
|
||||
db.users.newUser({
|
||||
return db.users.newUser({
|
||||
name,
|
||||
id,
|
||||
accessToken: access_token,
|
||||
@@ -50,14 +37,40 @@ router.get('/token', ({ query: { code } }, res) => {
|
||||
new Date().getTime() + expires_in * 1e3
|
||||
),
|
||||
})
|
||||
else return user
|
||||
})
|
||||
.then(({ name, id, perms }) => {
|
||||
const payload = {
|
||||
name,
|
||||
id,
|
||||
perms,
|
||||
},
|
||||
jwt = Jwt.sign(payload, secret)
|
||||
|
||||
res.end(
|
||||
[
|
||||
'<body>',
|
||||
'Logowanie...',
|
||||
'<br>',
|
||||
'Jeżeli to okno nie zamknie się automatycznie, spróbuj ponownie.',
|
||||
JSON.stringify({ name, id, perms }),
|
||||
'</body>',
|
||||
'<script>window.onload=function(){',
|
||||
`localStorage.setItem('token', "${jwt}");`,
|
||||
`localStorage.setItem('udata', '${JSON.stringify(
|
||||
payload
|
||||
)}');`,
|
||||
'window.close();',
|
||||
'}</script>',
|
||||
].join(' ')
|
||||
)
|
||||
})
|
||||
.catch((err) => {
|
||||
res
|
||||
.status(400)
|
||||
.end('There was an error processing your profile information')
|
||||
console.error(err)
|
||||
})
|
||||
.catch((err) => console.error(err))
|
||||
})
|
||||
.catch((err) => {
|
||||
res
|
||||
.status(400)
|
||||
.end('There was an error processing your profile information')
|
||||
console.error(err)
|
||||
})
|
||||
})
|
||||
.catch((err) => {
|
||||
|
||||
Reference in new issue
Block a user