I’m currently working on a recipe viewer website using Angular, and I’m looking to secure some sensitive data from being accessible in the client’s browser. The sensitive data includes service modules responsible for making HTTP requests to 3rd-party websites, JSON mappers, model classes, and environment variables including API keys.
I understand that Angular SSR is mainly used for SEO purposes and performance increase. But, I also know that any logic written on the server side will be hidden from the client’s browser. Given that my Angular app doesn’t use a traditional backend and thus contains business logic within itself, I’m considering using Angular SSR as a means to secure this sensitive data.
Here’s an example of a service module that I’d like to secure:
getRecipes(): Observable<Recipe[]> {
const params = new HttpParams()
.set('apiKey', this.apiKey)
.set('number', '16');
return this.http.get<any>(this.apiUrl + '/complexSearch', { params })
.pipe(
map(response => response.results.map(fromApiResponseForList)),
catchError(error => {
return of([]);
})
);
}
I’m wondering if anyone has experience with using Angular SSR in this way, and if there are any potential pitfalls or considerations I should be aware of. Any advice or guidance would be greatly appreciated.
Thank you in advance for your help!
2
Answers
Angular SSR is prerending on the backend but ships the bundle with the rendered HTML.
This won’t prevent you from sending the said code to the client.
When dealing with sensitive data that should not be visible to users, it is essential to keep it separate from your client-side code. While it’s technically possible to manipulate the HTML code sent to the user, doing so can be complex and error-prone. In this case, you need to write client-side code and server-side code, which need a lot of knowledge.
However, the easier way is extending your
server.ts
file, which handles SSR, with a new endpoint. Thereafter, you can call this endpoint directly than the other API. Below is an example with Angular 17.server.ts
app.component.ts
app.component.html
If you are familiar with Google Cloud Functions, also known as Firebase Functions, it is also a good way to use it. You can easily enhance the security of your application by integrating it with Secret Manager, a reliable tool for storing API keys and other sensitive information safely. This approach ensures that your keys are not exposed to users and provides an additional layer of protection for your application.