-
-
Notifications
You must be signed in to change notification settings - Fork 666
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
How to secure the backend api? #58
Comments
On the backend you need to add a new middleware to the endpoints you want to secure.
Of course you need to set that header somewhere once (I think at login). That header should be carried at each request. For further info search on google:
|
ok, great. i will work on that. thanks! |
On the client side: You need to use the AuthHttp with class from angular2-jwt instead of the basic Http class.
Then pass the AuthHttp dependency to the services:
On the server side: Add the express-jwt package from npm:
Add a catch all route to set the req.user property in app.ts:
Create a function to validate that req.user is set that you can call as middleware on your protected routes:
Hope that helps. |
@simon-hardy Quick question before I check through this, is AuthHttp able to handle https? Thanks in advance. |
Yep. |
that's sounds good, i'm trying to implement. just a few questions, because i'm new to this:
|
|
@simon-hardy probably some error that i did somewhere, but i can't figure out. it works anyways ;) |
for the current version i have solved it like this
|
You can also use express-jwt isLogged = expressJwt({ secret: process.env.SECRET_TOKEN, algorithms: ['HS256']}); router.get('/something', isLogged, userCtrl.something); I keep the issue open to let others see the solutions. |
I am thinking about how to test this. Do you have an idea? Maybe the test framework can intercept the "ensureAdminLoggedIn" function for this. I don't know about express-jwt |
I think express-jwt is tested itself so you don't need to but if you want more freedom I support the solution of @simon-hardy above and test just that function alone. |
What's the best way to secure the api? I mean to give separate permissions to each endpoint.
for example:
api/cats - open to anybody.
api/orders - available to admins only.
thanks.
The text was updated successfully, but these errors were encountered: