Token Developer Portal
Token AI Support
X Platform - Entegrasyon
X Platform - Entegrasyon
  • Token X Entegrasyon
  • Welcome
  • İş Akışları & Hazır Sepetler
  • Token X Connect (Wire)
    • Hızlı Başlangıç
    • Genel Tanıtım
    • Geliştirici Dokümanı
    • Örnek Uygulamalar
    • Sık Sorulan Sorular
    • Cihaz Bilgilendirme
    • Protokol Bilgileri
    • Sorun Tespit Aracı
  • Token X Connect (Cloud)
    • Genel Tanıtım (TR)
    • Geliştirici Dokümanı (TR)
    • TokenX API Integration (EN)
    • Automation Software Simulator
    • Sık Sorulan Sorular
  • Changelog
    • TokenX Connect
    • Backend
  • DESTEK
    • Geliştirici Destek
  • Test
    • Test Sürecimiz
  • APPSTORE
    • Tanıtım
Powered by GitBook
On this page
  • Authenticate
  • Logout
  • User Details
  • User Roles
  1. Token X admin panel integrations
  2. Authentication Integration
  3. Internal

Usage

Usage of library with examples.

Authenticate

Authenticate user with guard example.

import { SHooks } from '@token-public-org/token-sso-react-library';
import type { ReactNode } from 'react';

const { useSSO } = SHooks;

type Props = {
    children:ReactNode
}

const SecureGuard = ({children}:Props)=>{
    const { authenticated, login, initialized } = useSSO();
    
    // Wait until sso initialized
    if(!initialized){
        return <p>loading...</p>
    }

    // If authenticated return children
    if(authenticated){
        return children;
    }
    
    // Else redirect to login page
    login({
        redirectUri: window.location.href
    });
}

export default SecureGuard;
import { SHooks } from '@token-public-org/token-sso-react-library';

const { useSSO } = SHooks;

const SecureGuard = ({children})=>{
    const { authenticated, login, initialized } = useSSO();
    
    // Wait until sso initialized
    if(!initialized){
        return <p>loading...</p>
    }

    // If authenticated return children
    if(authenticated){
        return children;
    }
    
    // Else redirect to login page
    login({
        redirectUri: window.location.href
    });
}

export default SecureGuard;
  1. Guard render in dom,

  2. Check user authenticated,

    1. If authenticated render page.

    2. Else redirect to login page.

Logout

Logout with button example. (With redirect)

import { SHooks } from '@token-public-org/token-sso-react-library';

const { useSSO } = SHooks;

const AccountPage = ()=>{
    const { logout } = useSSO();

    const handleLogout = ()=>{
        // Logout and redirect back to origin
        logout({ redirectUri: encodeURI(window.location.origin) }); 
    }
    
    return(
        <button onClick={handleLogout}>Logout</button>
    )
}

export default AccountPage;
import { SHooks } from '@token-public-org/token-sso-react-library';

const { useSSO } = SHooks;

const AccountPage = ()=>{
    const { logout } = useSSO();

    const handleLogout = ()=>{
        // Logout and redirect back to origin
        logout({ redirectUri: encodeURI(window.location.origin) }); 
    }
    
    return(
        <button onClick={handleLogout}>Logout</button>
    )
}

export default AccountPage;

Logout with button example. (Without redirect)

import { SHooks } from '@token-public-org/token-sso-react-library';

const { useSSO } = SHooks;

const AccountPage = ()=>{
    const { logout } = useSSO();

    const handleLogout = ()=>{
        // Logout and dont make any redirect !
        logout({ withRedirect: false }); 
        
        // Do it anything...
    }
    
    return(
        <button onClick={handleLogout}>Logout</button>
    )
}

export default AccountPage;
import { SHooks } from '@token-public-org/token-sso-react-library';

const { useSSO } = SHooks;

const AccountPage = ()=>{
    const { logout } = useSSO();

    const handleLogout = ()=>{
        // Logout and dont make any redirect !
        logout({ withRedirect: false }); 
        
        // Do it anything...
    }
    
    return(
        <button onClick={handleLogout}>Logout</button>
    )
}

export default AccountPage;

User Details

Example to get userDetails from useSSO hook.

import { SHooks } from '@token-public-org/token-sso-react-library';

const { useSSO } = SHooks;

const AccountPage = ()=>{
    const { userDetails } = useSSO();
    
    ...mainpulate "userDetails" your own.
}

export default AccountPage;
import { SHooks } from '@token-public-org/token-sso-react-library';

const { useSSO } = SHooks;

const AccountPage = ()=>{
    const { userDetails } = useSSO();
    
    ...mainpulate "userDetails" your own.
}

export default AccountPage;

User Roles

Example to get userRoles from useSSO hook.

import { SHooks } from '@token-public-org/token-sso-react-library';

const { useSSO } = SHooks;

const AccountPage = ()=>{
    const { roles:userRoles } = useSSO();
    
    ...mainpulate "userRoles" your own.
}

export default AccountPage;
import { SHooks } from '@token-public-org/token-sso-react-library';

const { useSSO } = SHooks;

const AccountPage = ()=>{
    const { roles:userRoles } = useSSO();
    
    ...mainpulate "userRoles" your own.
}

export default AccountPage;

Last updated 9 months ago