New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
examples: add auth+acl example like we do in some of our apis #14
Comments
I'd love to understand how to go about this: do I add some kind of token on every method?
|
I don't have any sample code, but keep in mind webrpc-gen just code-generates the server code as a standard http.Handler interface. Meaning you can use any of Go's standard http middleware, or whatever the respective target you're building for (ie. nodejs). For a go server, you can use https://github.com/goware/jwtauth for an auth key, then write yourself a custom "ACL" middleware handler that checks the Thats it. Once you have this structure setup in your workflow, its extremely easy to add support for other methods, and to manage controls. The ACL code should be quite small too. |
Thanks for a very nice project! On the client side, is the preferred way to pass the auth token in the headers parameter in the service call?: Like so: const headers = {
Authorization: `Bearer ${await currentUser.getIdToken()}`,
}
const resp = await api.private(headers) |
@ottob that certainly works, but also since the generated api accepts a export * from './api.gen'
import { ArcadeumAPI as BaseArcadeumAPI } from './api.gen'
export class ArcadeumAPI extends BaseArcadeumAPI {
jwtAuth: string | undefined = undefined
constructor(hostname: string) {
super(hostname, window.fetch)
this.fetch = this._fetch
}
_fetch = (input: RequestInfo, init?: RequestInit): Promise<Response> => {
return new Promise<Response>((resolve, reject) => {
// automatically include jwt auth header to requests
// if its been set on the api client
const headers = {}
if (this.jwtAuth && this.jwtAuth.length > 0) {
headers['Authorization'] = `BEARER ${this.jwtAuth}`
}
// before the request is made
init!.headers = { ...init!.headers, ...headers }
window.fetch(input, init).then(resp => {
// after the request has been made..
resolve(resp)
}).catch(err => {
// request error
reject(err)
})
})
}
} |
Great! Im new to js/ts but that helps a lot, thanks. |
I tried using your code now but got an error on this line:
Have you patched your gen.ts file so edit: I'm using typescript 3.9.7 |
No description provided.
The text was updated successfully, but these errors were encountered: