forked from johncodeos-blog/SignInWithAppleBackendServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
63 lines (53 loc) · 1.71 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require('dotenv').config()
const express = require('express')
const app = express()
const fs = require('fs')
const bodyParser = require('body-parser')
const jwt = require('jsonwebtoken');
const getClientSecret = () => {
const time = new Date().getTime() / 1000; // Current time in seconds since Epoch
const privateKey = fs.readFileSync(process.env.PRIVATE_KEY_FILE);
const headers = {
kid: process.env.KEY_ID,
typ: undefined
}
const claims = {
'iss': process.env.TEAM_ID,
'iat': time, // The time the token was generated
'exp': time + 86400 * 180, // Token expiration date
'aud': 'https://appleid.apple.com',
'sub': process.env.SERVICE_ID,
}
token = jwt.sign(claims, privateKey, {
algorithm: 'ES256',
header: headers
});
return token
}
app.post('/callback', bodyParser.urlencoded({
extended: true
}), (req, res) => {
if (res.statusCode == 200) {
var returnURL = ""
var firstName = ""
var middleName = ""
var lastName = ""
var email = ""
if (req.body.hasOwnProperty('user')) {
const userdata = req.body.user
const user = JSON.parse(userdata)
firstName = '&first_name=' + user.name['firstName']
middleName = '&middle_name=' + user.name['middleName']
lastName = '&last_name=' + user.name['lastName']
email = '&email=' + user.email
}
var code = '&code=' + req.body.code
var clientSecret = '&client_secret=' + getClientSecret()
returnURL = '?success=true' + code + clientSecret + firstName + middleName + lastName + email
res.redirect(returnURL)
} else {
res.redirect('?success=false')
}
})
var port = process.env.PORT;
app.listen(port, () => console.log(`Your app is listening on port ` + port + '.'))