Created
January 6, 2024 14:42
-
-
Save tonylampada/808816976705abcdee3b7407362f4f63 to your computer and use it in GitHub Desktop.
This file contains 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
////////////////////////////////////////////////////////////////////// | |
// surface: handling http requests for login | |
// "app" is an express application | |
// "authenticationService" is a service that knows how to authenticate users | |
app.use(loggingMiddleware); | |
app.use(handleUnknownErrorsMiddleware); | |
app.post('/api/login', reqLogin); | |
function loggingMiddleware(req, res, next) { | |
res.on('finish', () => { | |
console.log(`INFO: Method ${req.method} called at URL ${req.originalUrl} - Status: ${res.statusCode}`); | |
}); | |
next(); | |
} | |
function handleUnknownErrorsMiddleware(err, req, res, next){ | |
console.error(`ERROR: An error occurred on method ${req.method} at URL ${req.originalUrl} - Error: ${err.message}`); | |
res.status(500).send('Server error occurred'); | |
} | |
async function reqLogin(req, res){ | |
const { login, password } = req.body; | |
if (!login || !password) { | |
return res.status(400).json({ error: 'Login and password are required' }); | |
} | |
const result = await authenticationService.authenticate(login, password); | |
if (result.success) { | |
res.status(200).json({ message: 'Authentication successful', token: result.token }); | |
} else { | |
res.status(401).json({ error: 'Authentication failed' }); | |
} | |
} | |
////////////////////////////////////////////////////////////////////// | |
// authenticationService | |
const {userDao, filters} = require('./adapters/databaseAdapter'); | |
const {userByToken} = require('./adapters/cacheAdapter') | |
const {isEqual} = filters | |
const {hash, randomToken} = require('some-crypto-thing') | |
async function authenticate(login, password) { | |
const user = await userDao.findWhere([isEqual("login", login), isEqual("password", hash(password))]); | |
if (user) { | |
const token = randomToken() | |
await userByToken.set(token, user) | |
return { success: true, token: token }; // Exemplo de token | |
} else { | |
return { success: false }; | |
} | |
} | |
async function getUserByToken(token) { | |
// exercise. think about it. | |
} | |
module.exports = { | |
authenticate, | |
getUserByToken | |
}; | |
////////////////////////////////////////////////////////////////////// | |
// databaseAdapter. | |
// in this case, persisting things to firebase | |
const admin = require('firebase-admin'); | |
const db = admin.firestore(); | |
class BaseDao { | |
constructor(collectionName) { | |
this.collection = db.collection(collectionName); | |
} | |
async findWhere(conditions) { | |
let query = this.collection; | |
for (const condition of conditions) { | |
query = query.where(condition.field, condition.operator, condition.value); | |
} | |
const snapshot = await query.get(); | |
if (snapshot.empty) { | |
return null; | |
} | |
let results = []; | |
snapshot.forEach(doc => results.push({ id: doc.id, ...doc.data() })); | |
return results.length === 1 ? results[0] : results; | |
} | |
} | |
const filters = { | |
isEqual(field, value){ | |
return {field, operator: '==', value} | |
} | |
} | |
module.exports = { | |
userDao: new BaseDao("users"), | |
filters: filters | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment