# Oauth 2.0 not working as excpectation

**URL:** <https://community.servicem8.com/t/oauth-2-0-not-working-as-excpectation/251>\
**Category:** API & SDK\
**Created:** [April 9, 2025, 4:37pm UTC](https://community.servicem8.com/t/oauth-2-0-not-working-as-excpectation/251 "2025-04-09T16:37:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![developer](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@developer](https://community.servicem8.com/u/developer)\
**Post date:** [April 9, 2025, 4:37pm UTC](https://community.servicem8.com/t/oauth-2-0-not-working-as-excpectation/251/1 "2025-04-09T16:37:36Z")

</div>

**Originally posted by ibitbytesoftDev**

I’m hitting the route that contain the code of my app.  
NodeJs:

// filepath: c:\Users\Beast\OneDrive\Desktop\Job\_Portal\Job\_Portal\_Backend\src\routes\authRoutes.js  
const express = require(‘express’);  
const axios = require(‘axios’);  
const router = express.Router();  
require(‘dotenv’).config();

const { SERVICEM8\_CLIENT\_ID, SERVICEM8\_CLIENT\_SECRET, SERVICEM8\_REDIRECT\_URI } = process.env;

// Redirect to ServiceM8 OAuth  
router.get(‘/auth/servicem8’, (req, res) =\> {  
console.log(‘Redirecting to ServiceM8 OAuth…’);  
const authUrl = new URL('[https://api.servicem8.com/oauth/authorize’)](https://api.servicem8.com/oauth/authorize'));

```auto
// Add required OAuth parameters
const params = {
    response_type: 'code',
    client_id: SERVICEM8_CLIENT_ID,
    redirect_uri: SERVICEM8_REDIRECT_URI,
    scope: 'vendor',
    state: Math.random().toString(36).substring(7), // Add state for security
    prompt: 'consent' // Force consent screen
};

// Add parameters to URL
authUrl.search = new URLSearchParams(params).toString();

console.log('Auth URL:', authUrl.toString());
res.redirect(authUrl.toString());

```

});

// Handle OAuth callback  
router.get(‘/auth/callback’, async (req, res) =\> {  
const { code, error, error\_description } = req.query;  
if (error) {  
console.error(‘OAuth Error:’, error, error\_description);  
return res.status(400).send(`OAuth Error: ${error_description}`);  
}  
if (!code) {  
return res.status(400).send(‘Authorization code is missing’);  
}  
console.log(‘Received code:’, code);  
// Proceed with token exchange…  
});

module.exports = router;

But instead of any consent page it is redirecting to serviceM8 dashboard and nothing else. what is the bug in my code.
