Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example code to add Page Logout during installation #93

Open
TheEyes007 opened this issue May 25, 2022 · 0 comments
Open

Example code to add Page Logout during installation #93

TheEyes007 opened this issue May 25, 2022 · 0 comments

Comments

@TheEyes007
Copy link

TheEyes007 commented May 25, 2022

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant