Skip to content

Example code to add Page Logout during installation #93

Open
@TheEyes007

Description

@TheEyes007

Hello,

here is a code to return through the api route the user information but also to allow the logout.

Client side route in js (example for reactjs) :

function logout(evt) { axios.get(routing.oauthuri).then((response)=>{ window.location.href = response.data.oauth_uri+'/api/logout?uri='+window.location.origin }); }

Have a good day.

 `/**
 * @Route("/api", name="app_api")
 * @return Response
 */
public function index(): Response
{
    $user = $this->security->getUser();
    $userInfo = [];
    $userInfo['username'] = $user->getId();
    $userInfo['nickname'] = $user->getUserIdentifier();
    $userInfo['email'] = $user->getEmail();
    $userInfo['realusername'] = $user->getUsername();
    $userInfo['roles'] = $user->getRoles();

    return new JsonResponse($userInfo);
}`

 /**
 * @Route("/api/logout", name="app_api_logout", methods="GET")
 * @param Request $request
 * @return RedirectResponse
 */
public function userLogoutAction(Request $request)
{
    $error = "";
    $user = $this->security->getUser()->getUserIdentifier();
    $uri = $request->query->get('uri').'/logout';

    $tokenObj = $this->em->getRepository(AccessToken::class)->findBy(['userIdentifier' => $user]);

    if(!empty($user)){
        if (!empty($tokenObj)) {
            foreach ($tokenObj as $token) {
                $refreshToken = $this->em->getRepository(RefreshToken::class)->findBy(['accessToken' => $token->getIdentifier()]);

                if (!empty($tokenObj)) {
                    foreach ($refreshToken as $rtoken) {
                        $rtoken->revoke();
                        $this->em->flush();
                    }
                    $token->revoke();
                    $this->em->flush();
                }

            }
        }

    } else {
        $error = "Can't find access token";
    }

    $response = new RedirectResponse($uri);
    $response->headers->clearCookie("PHPSESSID");
    return $response;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions