Next.js Discord

Discord Forum

How would I get the name of a user in my headers?

Answered
Southeastern blueberry bee posted this in #help-forum
Open in Discord
Southeastern blueberry beeOP
Right now my /api/user script is
export async function GET() {

    const response = await fetch("https://customprofiles.xyz/api/v1/user", {
        method: 'GET',
        headers: {
            'token': '...',
            'name': 'the name of the person'
        }
    });
    const data = await response.json();

    return Response.json({ data });

}


/[name]/page.tsx

const response = await fetch('https://customprofiles.xyz/api/user', {
    headers: {
      'name': params.name,
    },
  });
  const res = await response.json()


How would i get the params name in the /api/user script?
Answered by Ray
import {NextRequest} from 'next/server'

export async function GET(req: NextRequest) {
    const name = req.headers.get("name")
    const response = await fetch("https://customprofiles.xyz/api/v1/user", {
        method: 'GET',
        headers: {
            'token': 'token',
            'name': name
        }
    });
    const data = await response.json();

    return Response.json({ data });

}
View full answer

154 Replies

Answer
Southeastern blueberry beeOP
oop
s
i exposed my toke
n
gotta change it now
💀
lol
ok
i changed my reply
Southeastern blueberry beeOP
same
let me test ur thing
and if it works i will thank you
again
omg
@Ray
you are insanly smart
thank you so much ray
np
btw, why don't you just fetch the /api/v1 in the page component directly?
Southeastern blueberry beeOP
wait @Ray
when
i do
so when i go on the page.tsx
  console.log(res)
this prints
{ data: { profile: 'No profile found.' } }
when it should be printing all the info
the name of the profile is valid
see
@Ray any idea?
@Ray btw, why don't you just fetch the /api/v1 in the page component directly?
Southeastern blueberry beeOP
so they dont see token
@Southeastern blueberry bee so they dont see token
they won't see it if its a server component
Southeastern blueberry beeOP
yh but page.tsx is client
and the url endpoint
@Southeastern blueberry bee Click to see attachment
that's why the res is { data: { profile: 'No profile found.' }
if it's client component, you should do the fetching inside a useEffect
Southeastern blueberry beeOP
why
or react-query
Southeastern blueberry beeOP
o
how do i do that
like this
useEffect(() => {

    const f = async () => {
      try {
        const response = await fetch('https://customprofiles.xyz/api/user', {
          headers: {
            'name': params.name,
          },
        });
        const res = await response.json()
        setData(res)
      } catch(e) {
        setData('Error')
      }
    }
  }, [])

  console.log(res)
?
"use client";

import { useEffect } from "react";

export default function Page({ params }: { params: { name: string } }) {
  useEffect(() => {
    async function fetchData() {
      const response = await fetch("https://customprofiles.xyz/api/user", {
        headers: {
          name: params.name,
        },
      });
      const res = await response.json();
      console.log(res);
    }

    fetchData();
  }, [params.name]);

  return <div>1</div>;
}
Southeastern blueberry beeOP
data is null
i dont see the
console.log(res);
i got the result
Southeastern blueberry beeOP
@Ray i got the result
Southeastern blueberry beeOP
what page isthat?
?
whaats ur console on that page
i see the result here too
Southeastern blueberry beeOP
what
lol
Southeastern blueberry beeOP
i dont see that
so
not sure what happen on your side
try to remove async
Southeastern blueberry beeOP
now?
'use client'
import { useEffect, useState } from 'react'
import styles from '../components/custom.module.css'

export default function Page({ params }: { params: { name: string }}) {

  const [ data, setData ] = useState(null)

  useEffect(() => {
    async function fetchData() {
      const response = await fetch("https://customprofiles.xyz/api/user", {
        headers: {
          name: params.name,
        },
      });
      const res = await response.json();
      console.log(res);
      setData(res)
    }

    fetchData();
  }, [params.name]);

  return <div className={styles.container}>
    <div className={styles.logo}>
      <img src="https://cdn.discordapp.com/attachments/1178132493690474559/1180203036132528258/image.png?ex=6585cb3a&is=6573563a&hm=b6d399c87c67f14b3eca97adbd4566b634e8876529347c418931f9f1c63bc251&" width="70px" height="70px"></img>
      <p>Custom Profiles</p>
    </div>
  </div>
}
Southeastern blueberry beeOP
what
am i doing anything wrong on my console?
there is something wrong with the page loading the js on your side
I don't get those error
Southeastern blueberry beeOP
ill try on different browser
oh
it works on a different browser
cool
what broswer were you using
Southeastern blueberry beeOP
google
ah
Southeastern blueberry beeOP
now firefox
how do i access the data in here
store it in state
Southeastern blueberry beeOP
..
const [ data, setData ] = useState(null)
thats this
yea?
"use client";

import { useEffect, useState } from "react";

export default function Page({ params }: { params: { name: string } }) {
  const [user, setUser] = useState([]);

  useEffect(() => {
    async function fetchData() {
      const response = await fetch("https://customprofiles.xyz/api/user", {
        headers: {
          name: params.name,
        },
      });
      const res = await response.json();
      setUser(res);
    }

    fetchData();
  }, [params.name]);

  return <div>{JSON.stringify(user, null, 2)}</div>;
}
yea
which one is bio
Southeastern blueberry beeOP
only bio
"use client";

import { useEffect, useState } from "react";

export default function Page({ params }: { params: { name: string } }) {
  const [user, setUser] = useState([]);

  useEffect(() => {
    async function fetchData() {
      const response = await fetch("https://customprofiles.xyz/api/user", {
        headers: {
          name: params.name,
        },
      });
      const res = await response.json();
      setUser(res);
    }

    fetchData();
  }, [params.name]);

  if (!user) return <div>loading...</div>

  const [bioObj] = user

  return <div>{JSON.stringify(bioObj.bio, null, 2)}</div>;
}
Southeastern blueberry beeOP
uh
im confused from that
confuse on which part
Southeastern blueberry beeOP
const [bioObj] = user

return <div>{JSON.stringify(bioObj.bio, null, 2)}</div>;
because user is an array of 3 object
we destruct the first object
Southeastern blueberry beeOP
oh yh
but
why bioObj?
can be anyname lol
Southeastern blueberry beeOP
why does this not work
const [bioObj] = user

return <div>{JSON.stringify(bioObj, null, 2)}</div>;
what
does it work for you?
oh
const [bioObj] = user.data
Southeastern blueberry beeOP
o
still deosnt work
show the code
Southeastern blueberry beeOP
"use client";
import styles from '../components/custom.module.css'
import { useEffect, useState } from "react";

export default function Page({ params }: { params: { name: string } }) {
  const [user, setUser] = useState([]);

  useEffect(() => {
    async function fetchData() {
      const response = await fetch("https://customprofiles.xyz/api/user", {
        headers: {
          name: params.name,
        },
      });
      const res = await response.json();
      setUser(res);
    }

    fetchData();
  }, [params.name]);

  const [test] = user.data

  return <div className={styles.container}>
    <div className={styles.logo}>
      <img src="https://cdn.discordapp.com/attachments/1178132493690474559/1180203036132528258/image.png?ex=6585cb3a&is=6573563a&hm=b6d399c87c67f14b3eca97adbd4566b634e8876529347c418931f9f1c63bc251&" width="70px" height="70px"></img>
      <p>Custom Profiles</p>
    </div>
    <p>{JSON.stringify(test.bio, null, 2)}</p>
  </div>
}
oh wait
"use client";
import styles from '../components/custom.module.css'
import { useEffect, useState } from "react";

export default function Page({ params }: { params: { name: string } }) {
  const [user, setUser] = useState([]);

  useEffect(() => {
    async function fetchData() {
      const response = await fetch("https://customprofiles.xyz/api/user", {
        headers: {
          name: params.name,
        },
      });
      const res = await response.json();
      setUser(res.data);
    }

    fetchData();
  }, [params.name]);

  if (!user) return <div>loading....</div>

  const [test] = user

  return <div className={styles.container}>
    <div className={styles.logo}>
      <img src="https://cdn.discordapp.com/attachments/1178132493690474559/1180203036132528258/image.png?ex=6585cb3a&is=6573563a&hm=b6d399c87c67f14b3eca97adbd4566b634e8876529347c418931f9f1c63bc251&" width="70px" height="70px"></img>
      <p>Custom Profiles</p>
    </div>
    <p>{JSON.stringify(test.bio, null, 2)}</p>
  </div>
}
you missed the loading part
Southeastern blueberry beeOP
but if the data hasent loaded itll say loading for forever
ok
"use client";
import styles from "../components/custom.module.css";
import { useEffect, useState } from "react";

export default function Page({ params }: { params: { name: string } }) {
  const [user, setUser] = useState([]);

  useEffect(() => {
    async function fetchData() {
      const response = await fetch("https://customprofiles.xyz/api/user", {
        headers: {
          name: params.name,
        },
      });
      const res = await response.json();
      setUser(res.data);
    }

    fetchData();
  }, [params.name]);

  const [test] = user;

  return (
    <div className={styles.container}>
      <div className={styles.logo}>
        <img
          src="https://cdn.discordapp.com/attachments/1178132493690474559/1180203036132528258/image.png?ex=6585cb3a&is=6573563a&hm=b6d399c87c67f14b3eca97adbd4566b634e8876529347c418931f9f1c63bc251&"
          width="70px"
          height="70px"
        ></img>
        <p>Custom Profiles</p>
      </div>
      <p>{JSON.stringify(test?.bio, null, 2)}</p>
    </div>
  );
}
this
Southeastern blueberry beeOP
omg
so
if i want the userid
i do
test?.userid
Southeastern blueberry beeOP
yh
so what if i want data from a different array
in the data
const [test,test2,test3] = user;
Southeastern blueberry beeOP
right
so it would be better to do
const [ info, status, user ]
cause they match the coresponding tables
yea its up to you
Southeastern blueberry beeOP
const [user, setUser] = useState([]);

  useEffect(() => {
    async function fetchData() {
      const response = await fetch("https://customprofiles.xyz/api/user", {
        headers: {
          name: params.name,
        },
      });
      const res = await response.json();
      setUser(res.data);
    }

    fetchData();
  }, [params.name]);

  const [ info, status, discorduser ] = user;

  console.log(info)
uh
nv,m
nvm again
what
it keep prints undefined then the array
@Ray
const [ info, status, discorduser ] = user;

console.log(discorduser.displayAvatarURL)
Southeastern blueberry beeOP
fixed