Last active
December 29, 2017 00:38
-
-
Save LoganBarnett/2cdb38511fce10ba596f781e961fffe1 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 handler = <T>( | |
users: Array<T>, | |
metaData: Array<MockResponse>, | |
authFn: (Auth, T) => bool, | |
respondFn: (T) => Object, | |
req: $Request, | |
res: $Response, | |
) => { | |
const eitherAuth = validateAuth(req.body) | |
if (eitherAuth instanceof Error) { | |
res.send(400) | |
} | |
else { | |
const maybeUser = users.find(authFn.bind(null, eitherAuth)) | |
if (!maybeUser) { | |
res.send(401) // or whatever code is the right one | |
} else { | |
res.send(200) | |
} | |
} | |
} | |
// usage | |
app.post('/auth/v1/students', handler.bind(null, students, (auth: Auth, s: StudentAuthenticationRequest) => { | |
return s.username == auth.username && s.password == auth.password | |
}, (s: StudentAuthenticationRequest) => ({ studentBid: s.studentBid })) | |
app.post('/auth/v1/test-sessions', handler.bind(null, testSessions, (auth: Auth, t: TestSessionAuthenticationRequest) => { | |
return t.sessionName == auth.username && t.pin == auth.password | |
}, (t: TestSessionAuthenticationRequest) => ({ testSessionBid: t.testSessionBid }))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment