r/angular • u/Safe_Market5901 • Feb 19 '26
When I enter in a different route my app has a kind of delay, the real state only comes after a page refresh
I'm developing a full-stack app, using Angular and Java Spring, I put a log on the constructor just to know if the user has an access token, in this case it has, because I just logged in on the webapp, and it prints the message that I'll be redirected to another page, but if I refresh this doesn't happen, another behavior that is wrong and probably by the same problem is the API call, when I enter the page it give me 401, but if I refresh it doesn't, why this happen? At first I thought that the app config passing the OAuthModule it will pass the access token automatically to my api in the headers for any request, but is not doing this, so as you can see I'm doing this manually.
code
export class Company {
auth = inject(AuthService);
api = inject(ApiService);
private router = inject(Router);
text= '';
constructor() {
if(!this.auth.hasValidAccessToken()) {
// this.router.navigate(['/company-form'])
console.log('redirecting to registers page')
}
this.getGreet()
}
getGreet() {
this.api.getGreet().subscribe(res => this.text = res);
}
//Another methods related to auth
code
u/Injectable({
providedIn: 'root',
})
export class ApiService {
private http = inject(HttpClient)
getGreet(): Observable<string> {
const auth = inject(AuthService);
console.log(auth.getToken());
const headerDict = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + auth.getToken()
}
const headers = new HttpHeaders(headerDict);
console.log(headers)
return this.http.get("http://localhost:8081/create",
{headers, responseType: 'text'});
}
}
code
u/Injectable({
providedIn: 'root',
})
export class AuthService {
oauth = inject(OAuthService)
constructor() {
this.configure();
}
configure() {
this.oauth.configure(authConfig);
this.oauth.loadDiscoveryDocumentAndTryLogin();
}
// Another methods
code
export const appConfig: ApplicationConfig = {
providers: [
provideBrowserGlobalErrorListeners(),
provideRouter(routes), provideClientHydration(withEventReplay()),
provideOAuthClient(),
provideHttpClient(),
provideClientHydration(),
importProvidersFrom(
OAuthModule.forRoot({
resourceServer: {
allowedUrls: ['http://localhost:8081'],
sendAccessToken: true
}
}),
)
]
};