2525// Algorithm:
2626// 1. Try to get the full Ory session from context via session.GetSession.
2727// - If an error occurs other than ErrSessionNotFound, it bubbles up.
28- // 2. If the session exists, attach its ID to metadata under "user-id".
29- // 3. If the session is missing, fall back to user-id stored separately in context.
28+ // 2. If the session exists and carries an identity, attach that identity ID under "user-id".
29+ // 3. If the session is missing (or does not include identity information), fall back to the user-id
30+ // stored separately in context.
3031// - If user-id is also missing or empty, return an error.
3132// 4. Return a new context containing metadata with the resolved user identifier.
3233func attachUserMetadata (ctx context.Context ) (context.Context , error ) {
@@ -35,8 +36,12 @@ func attachUserMetadata(ctx context.Context) (context.Context, error) {
3536 return nil , fmt .Errorf ("%w: %w" , ErrFailedToGetSession , err )
3637 }
3738
38- if sess != nil && sess .GetId () != "" {
39- return metadata .AppendToOutgoingContext (ctx , userIDKey , sess .GetId ()), nil
39+ if sess != nil {
40+ if identity , ok := sess .GetIdentityOk (); ok && identity != nil {
41+ if identityID := identity .GetId (); identityID != "" {
42+ return metadata .AppendToOutgoingContext (ctx , userIDKey , identityID ), nil
43+ }
44+ }
4045 }
4146
4247 userID , err := session .GetUserID (ctx )
0 commit comments