Created
May 6, 2022 06:10
-
-
Save akalana/8c226427183b48a3722ac4d73a0510f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
var cors = require('cors'); | |
const app = express(); | |
app.use(cors({ | |
origin: 'http://localhost:3000', | |
credentials: true, | |
})); | |
app.use(express.json()); | |
// const keycloak = require('./config/keycloak-config.js').initKeycloak(); | |
// app.use(keycloak.middleware()); | |
const Keycloak = require('keycloak-connect'); | |
const session = require('express-session'); | |
const { default: axios } = require('axios'); | |
const { default: KeycloakAdminClient } = require('keycloak-admin'); | |
const keycloakConfig = { | |
clientId: 'slocoach-backend-client', | |
bearerOnly: true, | |
serverUrl: 'http://localhost:8090/auth', | |
realm: 'slocoach', | |
credentials: { | |
secret: '9ee1efa2-3087-4c45-b347-e1746af1e381' | |
} | |
}; | |
const keycloakAdminConfig = { | |
grantType: 'password', | |
clientId: 'master-backend-client', | |
clientSecret: '0d4d74a1-3f91-4769-a584-a08ed5abd51f', | |
username: 'admin', | |
password: 'jncsgat237bdew7' | |
}; | |
let admin = new KeycloakAdminClient({ | |
baseUrl: 'http://localhost:8090/auth', | |
realmName: 'master', | |
}); | |
admin.auth(keycloakAdminConfig); | |
admin.setConfig({ | |
realmName: 'slocoach' | |
}); | |
var memoryStore = new session.MemoryStore(); | |
var keycloak = new Keycloak({ store: memoryStore }, keycloakConfig); | |
//session | |
app.use(session({ | |
secret: 'ECC3A9BWRwADB11B11Gl8yhirbXIRDXY', | |
resave: false, | |
saveUninitialized: true, | |
store: memoryStore | |
})); | |
app.use(keycloak.middleware()); | |
// var authControler = require('./controller/auth-controller'); | |
// app.use('/api/auth', authControler); | |
app.post('/api/auth/sign-in', async function (req, res) { | |
try { | |
let url = 'http://localhost:8090/auth/realms/slocoach/protocol/openid-connect/token'; | |
const { email, password } = req.body; | |
const params = new URLSearchParams(); | |
params.append('client_id', 'slocoach-backend-client'); | |
params.append('client_secret', '9ee1efa2-3087-4c45-b347-e1746af1e381'); | |
params.append('username', email); | |
params.append('password', password); | |
params.append('grant_type', 'password'); | |
const { data } = await axios.post(url, params, { | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded' | |
} | |
}); | |
const response = { | |
tokens: { | |
accessToken: data.access_token, | |
expiresIn: data.expires_in, | |
refreshToken: data.refresh_token, | |
refreshExpiresIn: data?.refresh_expires_in, | |
} | |
} | |
res.send(response); | |
} catch (error) { | |
res.status(500).send(JSON.stringify(error)); | |
} | |
}); | |
app.post('/api/auth/sign-up', async function (req, res) { | |
try { | |
const { email, password, firstName, lastName, phoneNumber } = req.body; | |
const response = await admin.users.create({ | |
username: email, | |
email: email, | |
enabled: true, | |
credentials: [{ | |
temporary: false, | |
type: 'password', | |
value: password, | |
}], | |
firstName: firstName, | |
lastName: lastName, | |
}); | |
res.send(response); | |
} catch (error) { | |
res.status(500).send(JSON.stringify(error)); | |
} | |
}); | |
app.post('/api/auth/sign-out', async function (req, res) { | |
try { | |
let url = 'http://localhost:8090/auth/realms/slocoach/protocol/openid-connect/logout'; | |
const { refreshToken } = req.body; | |
const params = new URLSearchParams(); | |
params.append('client_id', 'slocoach-backend-client'); | |
params.append('client_secret', '9ee1efa2-3087-4c45-b347-e1746af1e381'); | |
params.append('refresh_token', refreshToken); | |
const response = await axios.post(url, params, { | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded' | |
} | |
}); | |
res.send(JSON.stringify(response)); | |
} catch (error) { | |
res.status(500).send(JSON.stringify(error)); | |
} | |
}); | |
app.get('/', function (req, res) { | |
res.send("Server is up!"); | |
}); | |
app.listen(5000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment