Auth Module

The Auth Module provides authentication-related features in your Medusa and Node.js applications.

How to Use Auth Module's Service#

Use the Auth Module's main service by resolving from the Medusa container the resource Modules.AUTH imported from @medusajs/framework/utils.

For example:


Features#

Basic User Authentication#

Authenticate users using their email and password credentials.

Code
1const { success, authIdentity, error } = await authModuleService.authenticate(2  "emailpass",3  {4    url: req.url,5    headers: req.headers,6    query: req.query,7    body: req.body,8    authScope: "admin",9    protocol: req.protocol,10  } as AuthenticationInput11)12
13if (!success) {14  // incorrect authentication details15  throw new Error(error)16}

Third-Party and Social Authentication#

The Auth Module supports a variety of authentication methods, such as authenticating with third-party services and social platforms.

Code
1// in authentication API route2const { success, authIdentity, location } =3  await authModuleService.authenticate("google", {4    url: req.url,5    headers: req.headers,6    query: req.query,7    body: req.body,8    authScope: "admin",9    protocol: req.protocol,10  } as AuthenticationInput)11
12if (location) {13  return res.json({ location })14}15
16// in callback API route17const { success, authIdentity } = await authModuleService.validateCallback(18  "google",19  {20    url: req.url,21    headers: req.headers,22    query: req.query,23    body: req.body,24    authScope: "admin",25    protocol: req.protocol,26  } as AuthenticationInput27)

Configure Auth Module#

Refer to this documentation for details on the module's options.

Was this page helpful?
Edit this page