Skip to content

Token returned from getOAuthToken is unusable #112

@louis030195

Description

@louis030195

My users connect their Neurosity account by going to an url created with:

// server ...

import { Neurosity } from "@neurosity/sdk";
import { cookies } from 'next/headers'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

const neurosity = new Neurosity({
    autoSelectDevice: false
});
export async function GET(request: NextRequest) {
    return neurosity
        .createOAuthURL({
            clientId: process.env.NEUROSITY_OAUTH_CLIENT_ID!,
            clientSecret: process.env.NEUROSITY_OAUTH_CLIENT_SECRET!,
            redirectUri: process.env.NEUROSITY_OAUTH_CLIENT_REDIRECT_URI!,
            responseType: "token",
            state: Math.random().toString().split(".")[1], // A random string is required for security reasons
            scope: [
                "read:devices-info",
                "read:devices-status",
                "read:signal-quality",
                "read:brainwaves",
                "read:focus",
            ]
        })
        .then((url) => NextResponse.json({url: url}))
        .catch((error) => NextResponse.json({ error: error.response.data }, { status: 400 }))
}


// ... later on client

const neurosity = new Neurosity();

console.log(token);

await neurosity.login({ customToken: token });

neurosity.focus().subscribe(console.log)
// works
{
  label: 'focus',
  metric: 'awareness',
  probability: 0.7761862422393826,
  timestamp: 1691170018286
}

OAuth tokens expire after 60 minutes, so I renew them with getOAuthToken.
When I try to use the token from getOAuthToken it fails:

const { Neurosity } = require("@neurosity/sdk");
const fs = require('fs');
const dotenv = require("dotenv");
dotenv.config();

main();

async function main() {

    const neurosity = new Neurosity();

    const token = await neurosity.getOAuthToken({
        clientId: process.env.NEUROSITY_OAUTH_CLIENT_ID!,
        clientSecret: process.env.NEUROSITY_OAUTH_CLIENT_SECRET!,
        userId: userId,
    });

    console.log(token);

    await neurosity.login({ customToken: token });

    neurosity.focus().subscribe(console.log)
}
            throw err;
            ^

Error: Neurosity SDK: You are trying to access data with an OAuth token without access to the following scopes: read:focus.
    at getScopeError (/Users/louisbeaumont/Documents/hello-world/node_modules/@neurosity/sdk/dist/cjs/utils/oauth.js:92:12)
    at validateOAuthScopeForFunctionName (/Users/louisbeaumont/Documents/hello-world/node_modules/@neurosity/sdk/dist/cjs/utils/oauth.js:88:19)
    at Neurosity.focus (/Users/louisbeaumont/Documents/hello-world/node_modules/@neurosity/sdk/dist/cjs/Neurosity.js:857:91)
    at main (/Users/louisbeaumont/Documents/hello-world/oauth.js:22:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v20.0.0

If I use the first token it works. Seems to be an unexpected behaviour? What is the usage of the new token?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions