useDirectusAuth


Check out the Directus Authentication documentation.

login

Authentication is submitted with email and password of the user to the given directus server. Sets user and token.

pages/login.vue
<script setup lang="ts">const { login } = useDirectusAuth();const router = useRouter();const onSubmit = async () => {  try {    await login({ email: "", password: "" });  } catch (e) {}};</script>

logout

Reset user and token.

pages/home.vue
<script setup lang="ts">const { logout } = useDirectusAuth();const onSubmit = async () => {  logout();};</script>

createUser

Create a new Directus user, can also be used as register(). Email and password are required, partial user object is given.

pages/register.vue
<script setup lang="ts">const { createUser } = useDirectusAuth();const router = useRouter();const onSubmit = async () => {  try {    const newUser = await createUser({ email: "", password: "" });  } catch (e) {}};</script>

requestPasswordReset

Password Request is submitted with email of the user to the given directus server.

<script setup lang="ts">const { requestPasswordReset } = useDirectusAuth();const router = useRouter();const onSubmit = async () => {  try {    await requestPasswordReset({ email: "" });  } catch (e) {}};</script>

resetPassword

Password reset is submitted with token of reset email & the new password to the given directus server.

<script setup lang="ts">const { resetPassword } = useDirectusAuth();const router = useRouter();const onSubmit = async () => {  try {    await resetPassword({ token: "", password: "" });  } catch (e) {}};</script>

inviteUser

Invite a user to join Directus. Email and role are required. Email is sent by directus server to email with a link to directus server or invite_url containing invite token.

<script setup lang="ts">const { inviteUser } = useDirectusAuth();const router = useRouter();const onSubmit = async () => {  try {    await inviteUser({ email: "", role: "c86c2761-65d3-43c3-897f-6f74ad6a5bd7" });  } catch (e) {}};</script>

acceptInvite

Accepts an invitation created via inviteUser. Token and password are required. Token is provided as a query param in the invite_url.

<script setup lang="ts">const { acceptInvite } = useDirectusAuth();const router = useRouter();const onSubmit = async () => {  try {    await acceptInvite({ token: "", password: "" });  } catch (e) {}};</script>

Middleware example

Check how to Redirect user to login page over here.