Is there any way to create a session in kratos pro...
# ory-selfhosting
d
Is there any way to create a session in kratos programatically? e.g. for automated tests? As far as I can tell both browser and native flows require a browser to exist.
m
Native flows would not use a browser. You can test browser flows with something like Playwright. Not sure what the equivalent is for native 🤔
d
Wrote this for anyone for future reference:
Copy code
const getUserToken = async (): Promise<string> => {
  const flow = await frontendAPI.createNativeLoginFlow();

  const response = await fetch(flow.data.ui.action, {
    method: 'POST',
    body: `csrf_token=&identifier=${SAMPLE_EMAIL}&password=${SAMPLE_PASSWORD}&method=password`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  });

  const session = await response.json();

  return session?.session_token || '';
}
b
@damp-fountain-38244 Why not use the
updateNativeLoginFlow
method instead of using fetch?
d
I did switch over to that:
Copy code
const getUserToken = async (): Promise<string> => {
  const flow = await frontendAPI.createNativeLoginFlow();

  const response = await frontendAPI.updateLoginFlow({
    flow: flow.data.id,
    updateLoginFlowBody: {
      identifier: SAMPLE_EMAIL,
      password: SAMPLE_PASSWORD,
      method: 'password'
    }
  })

  const session = response.data;

  return session?.session_token || '';
}