import type { Ref } from 'vue' ; import type { StrapiAuthenticationData , StrapiAuthenticationResponse , StrapiAuthProvider , StrapiEmailConfirmationData , StrapiForgotPasswordData , StrapiRegistrationData , StrapiResetPasswordData , StrapiUser } from '../types' ; import { useStrapiToken } from './useStrapiToken' ; import { useStrapiUser } from './useStrapiUser' ; import { useStrapiClient } from './useStrapiClient' ; import { useStrapiUrl } from './useStrapiUrl' ; export const useStrapiAuth = ( ) => { const url = useStrapiUrl ( ) ; const token = useStrapiToken ( ) ; const user = useStrapiUser ( ) ; const client = useStrapiClient ( ) ; const setToken = ( value ) => { token . value = value ; } ; const setUser = ( value ) => { user . value = value ; } ; const fetchUser = async ( ) : Promise < Ref < StrapiUser > > => { if ( token . value && ! user . value ) { try { user . value = await client ( '/users/me' ) ; } catch ( e ) { setToken ( null ) ; } } return user ; } ; param { StrapiAuthenticationData } data param { string } data . identifier param { string } data . password returns const login = async ( data ) : Promise < StrapiAuthenticationResponse > => { setToken ( null ) ; const { jwt } = await client ( '/auth/local' , { method : 'POST' , body : data } ) ; setToken ( jwt ) ; const user = await fetchUser ( ) ; return { user , jwt } ; } ; returns const logout = ( ) : void => { setToken ( null ) ; setUser ( null ) ; } ; param { StrapiRegistrationData } data param { string } data . username param { string } data . email param { string } data . password returns const register = async ( data ) : Promise < StrapiAuthenticationResponse > => { setToken ( null ) ; const { jwt } = await client ( '/auth/local/register' , { method : 'POST' , body : data } ) ; setToken ( jwt ) ; const user = await fetchUser ( ) ; return { user , jwt } ; } ; param { StrapiForgotPasswordData } data param { string } data . email returns const forgotPassword = async ( data ) : Promise < void > => { setToken ( null ) ; await client ( '/auth/forgot-password' , { method : 'POST' , body : data } ) ; } ; param { StrapiResetPasswordData } data param { string } data . code param { string } data . password param { string } data . passwordConfirmation returns const resetPassword = async ( data ) : Promise < StrapiAuthenticationResponse > => { setToken ( null ) ; const { jwt } = await client ( '/auth/reset-password' , { method : 'POST' , body : data } ) ; setToken ( jwt ) ; const user = await fetchUser ( ) ; return { user , jwt } ; } ; param { StrapiEmailConfirmationData } data param { string } data . email returns const sendEmailConfirmation = async ( data ) : Promise < void > => { await client ( '/auth/send-email-confirmation' , { method : 'POST' , body : data } ) ; } ; param { StrapiAuthProvider } provider returns const getProviderAuthenticationUrl = ( provider ) : string => { return new URL ( `/connect/${ provider }` , url ) . href ; } ; param { StrapiAuthProvider } provider param { string } access_token returns const authenticateProvider = async ( provider , access_token ) : Promise < StrapiAuthenticationResponse > => { setToken ( null ) ; const { jwt } = await client ( `/auth/${ provider }/callback` , { method : 'GET' , params : { access_token } } ) ; setToken ( jwt ) ; const user = await fetchUser ( ) ; return { user , jwt } ; } ; return { setToken , setUser , fetchUser , login , logout , register , forgotPassword , resetPassword , sendEmailConfirmation , getProviderAuthenticationUrl , authenticateProvider } ; } ;