Angular & PHP Quickstart
This is a quickstart guide to help you integrate Descope with your Angular & PHP application. Follow the steps below to get started.
Install Frontend SDK
Install the SDK with the following command:
npm i --save @descope/angular-sdkyarn add @descope/angular-sdkpnpm add @descope/angular-sdkbun i @descope/angular-sdkThen, add Descope type definitions to your tsconfig.ts
"compilerOptions": {
"typeRoots": ["./node_modules/@descope"],
// ... other options
}Import and Wrap Application with Descope Angular SDK
Start by importing Descope then add the Angular SDK to your application. You will need your Project ID from Project Settings in the Descope Console.
Note
You can also add the optional baseUrl parameter if you're utilizing a custom domain within your Descope project (ex: https://auth.company.com).
import { NgModule, APP_INITIALIZER} from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { DescopeAuthModule, DescopeAuthService, descopeInterceptor } from '@descope/angular-sdk';
import { AppComponent } from './app.component';
import {
HttpClientModule,
provideHttpClient,
withInterceptors
} from '@angular/common/http';
import { zip } from 'rxjs';
export function initializeApp(authService: DescopeAuthService) {
return () => zip([authService.refreshSession(), authService.refreshUser()]);
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
DescopeAuthModule.forRoot({
projectId: 'YOUR_PROJECT_ID'
})
],
providers: [
{
provide: APP_INITIALIZER,
useFactory: initializeApp,
deps: [DescopeAuthService],
multi: true
},
provideHttpClient(withInterceptors([descopeInterceptor]))
],
bootstrap: [AppComponent]
})
export class AppModule { }Optional Client Auth Settings
You can customize client-side token behavior using optional parameters like persistTokens, and sessionTokenViaCookie. Learn more in the Auth Helpers documentation.
Import Descope Functions and Add Flows Component
To trigger the Descope Flow, you will need to add this component in your Angular template. The screens you've customized in your Flow will appear here. You can also customize the component with the following:
flowId: ID of the flow you wish to useonSuccessandonError: functions that execute when authentication succeeds or fails
Note
For the full list of component customization options, refer to our Descope Components Doc.
<div style="max-width: 1000px; margin: 0 auto; place-items: center;">
<descope
flowId="sign-up-or-in"
(success)="onSuccess($event)"
(error)="onError($event)"
></descope>
</div>Utilize the Angular SDK Hooks and Functions
Descope provides many different hooks to check if the user is authenticated, session is loading etc. You can use these to customize the user experience:
isAuthenticated: Boolean for the authentication state of the current user session.user: Returns information about the currently authenticated user.logout(): Logs out the currently authenticated user.
Note
For the full list of available hooks and functions, refer to the Auth Helpers Doc.
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { DescopeAuthService } from '@descope/angular-sdk';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
isAuthenticated: boolean = false;
token: string = "";
constructor(
private router: Router,
private authService: DescopeAuthService
) {}
ngOnInit() {
this.authService.session$.subscribe((session) => {
this.isAuthenticated = session.isAuthenticated;
});
this.authService.user$.subscribe((descopeUser) => {
if (descopeUser.user) {
this.userName = descopeUser.user.name ?? '';
}
});
}
logout() {
this.authService.descopeSdk.logout();
}
}At this point, you're done with the frontend. If you would like to also handle Session Management in your backend, keep on reading!
Install Backend SDK
Install the SDK with Composer using the following command:
composer require descope/descope-phpSet up Environment file
Create a .env file in the root directory of your project with your Descope Project ID, which can be found in the Console
If you plan to use Management functions, include a Descope Management Key here as well, which can be found here.
DESCOPE_PROJECT_ID=<Descope Project ID>
DESCOPE_MANAGEMENT_KEY=<Descope Management Key>Setup Backend SDK
You'll need to initialize a DescopeSDK object using your Project ID.
If you're using a custom domain with your Descope project, make sure to export the Base URL (e.g. export DESCOPE_BASE_URI="https://api.descope.com") when initializing descope_client.
require 'vendor/autoload.php';
use Descope\SDK\DescopeSDK;
$descopeSDK = new DescopeSDK([
'projectId' => $_ENV['DESCOPE_PROJECT_ID'],
'managementKey' => $_ENV['DESCOPE_MANAGEMENT_KEY'] // Optional, only used for Management functions
'baseUrl' => $_ENV['DESCOPE_BASE_URL'], // Optional, only used for custom domain or different regions
]);Implement Session Validation
You will need to then fetch the session token from the Authorization header of each request, and use the SDK to validate the token.
The frontend SDK will store the session token in either a cookie or your browser's local storage. If using a cookie, the token will be sent to your app server automatically with every request.
The $descopeSDK->verify($sessionToken) function can be used to verify a user's session as shown below. This returns a TRUE or FALSE depending on if the JWT is valid or not.
if (isset($_POST["sessionToken"])) {
if ($descopeSDK->verify($_POST["sessionToken"])) {
$_SESSION["user"] = json_decode($_POST["userDetails"], true);
$_SESSION["sessionToken"] = $_POST["sessionToken"];
session_write_close();
// User session validated and token saved
exit();
} else {
error_log("Session token verification failed.");
$descopeSDK->logout();
// Redirect to login page
exit();
}
} else {
error_log("Session token is not set in POST request.");
// Redirect to login page
exit();
}Checkpoint
Your application is now integrated with Descope. Please test with sign-up or sign-in use case.
Customize
Now that you have the end-to-end application working, you can choose to configure and personalize many different areas of Descope, including your brand, style, custom user authentication journeys, etc. We recommend starting with customizing your user-facing screens, such as signup and login.