@@ -313,16 +313,20 @@ and add the following function.
313313``` elixir
314314 def welcome (conn, _params ) do
315315
316- # Check if there's a session token
317- case conn |> get_session (:token ) do
316+ # Check if there's a user_id in the session
317+ case conn |> get_session (:user_id ) do
318318
319319 # If not, we redirect the person to the login page
320320 nil ->
321321 conn |> redirect (to: " /" )
322322
323- # If there's a token, we render the welcome page
324- token ->
325- {:ok , profile} = ElixirAuthMicrosoft .get_user_profile (token.access_token)
323+ # If there's a user_id, we render the welcome page with stored user info
324+ user_id ->
325+ profile = %{
326+ id: user_id,
327+ displayName: get_session (conn, :user_name ),
328+ userPrincipalName: get_session (conn, :user_email )
329+ }
326330
327331 conn
328332 |> put_view (AppWeb .PageView )
@@ -333,12 +337,12 @@ and add the following function.
333337
334338We are using the
335339[ ` get_session ` ] ( https://hexdocs.pm/plug/Plug.Conn.html#get_session/2 )
336- to retrieve the ` token ` from the session.
340+ to retrieve the user information from the session.
337341We've * yet* to place it there in the first place,
338342but don't worry, we'll do it next!
339- If no ` token ` is found,
343+ If no ` user_id ` is found,
340344we redirect the person to the homepage to login.
341- If it is, we render the page normally!
345+ If it is, we construct a profile map from the stored session data and render the page normally!
342346
343347Now let's put the ` token ` in the session
344348after the person logs in successfully.
@@ -355,25 +359,38 @@ change the `index` function to the following:
355359 end
356360
357361 {:ok , token} = ElixirAuthMicrosoft .get_token (code, conn)
362+ {:ok , profile} = ElixirAuthMicrosoft .get_user_profile (token.access_token)
358363
359-
364+ # Store only essential user info to avoid cookie overflow
365+ # Azure AD tokens can be 8KB+ for users with many group memberships.
366+ # Alternatively, you can store the entire token.
367+ # |> put_session(:token, token)
360368 conn
361- |> put_session (:token , token)
369+ |> put_session (:user_id , profile.id)
370+ |> put_session (:user_email , profile.mail || profile.userPrincipalName)
371+ |> put_session (:user_name , profile.displayName)
362372 |> redirect (to: " /welcome" )
363373 end
364374```
365375
366- We are simply using the
376+ We are using the
367377[ ` put_session ` ] ( https://hexdocs.pm/plug/Plug.Conn.html#put_session/3 )
368- function to persist the token within the connection session
369- to later be retrieved by the page
370- after successful login.
378+ function to persist only the essential user information in the session.
379+
380+ > [ !WARNING]
381+ >
382+ > We store only the user's ID, email, and name instead of the entire token object.
383+ > This prevents ` Plug.Conn.CookieOverflowError ` which occurs when cookies exceed 4096 bytes.
384+ > Microsoft/Azure AD tokens can be very large (8KB+), especially for users who are members
385+ > of many Azure AD groups.
386+ > You can choose to store the entire token, but be aware of potential cookie size issues.
387+
371388The person is redirected to the ` /welcome ` page
372389we've defined earlier if they manage to login.
373390
374391And that's it!
375392If you refresh the ` /welcome ` page,
376- the token won't be lost! 🎉
393+ the user info won't be lost! 🎉
377394
378395## 7. Logging out
379396
@@ -443,16 +460,18 @@ Open the file and add the following function:
443460``` elixir
444461 def logout (conn, _params ) do
445462
446- # Clears token from user session
463+ # Clears all user data from session
447464 conn
448- |> delete_session ( :token )
465+ |> clear_session ( )
449466 |> redirect (to: " /" )
450467 end
451468```
452469
453- We are simply clearing the person's session
470+ We are simply clearing the person's entire session
454471and redirecting them to the homepage
455472(so they can log in again, if they wish to).
473+ Using ` clear_session() ` ensures all session data is removed,
474+ not just specific keys.
456475
457476
458477### 7.2 Adding a button so the person logs out
0 commit comments